6

Linq-to-NHibernateを使用して次のSQL出力を取得しようとしています。

SELECT DISTINCT Name, at.Year FROM MyTable mt
INNER JOIN AnotherTable at ON at.Id = mt.AnotherTableId

NameプロパティとYearプロパティは新しいクラスにラップされるため、C#コードは次のようになります。

Session.Linq()
   .Select(x => new FooBar { Name = x.Name, Year = x.AnotherTable.Year }))
   .ToList();

DISTINCTキーワードをSQLクエリに表示するにはどうすればよいですか?

4

2 に答える 2

1

Can't your try:

Session.Linq()
   .Select(x => new FooBar { Name = x.Name, Year = x.Year }))
   .Distinct()
   .ToList();

Select returns an IEnumerable, so by default it should have Distinct, regardless of whether your intellisense detects it or not.

于 2009-11-16T04:36:47.130 に答える
1

私は日常的にlinqプロバイダーを使用していませんが、それを見回すことは不可能のようです.

QueryOver を検討していただけますか。

 First firstReference = null;
        Second secondReference = null;
        var items = Session().QueryOver(() => firstReference)
                        .JoinAlias(() => firstReference.Seconds, () => secondReference)
                        .Select(Projections.Distinct(
                                Projections.ProjectionList()
                                    .Add(Projections.Property(() => firstReference.Name).WithAlias(() => firstReference.Name))
                                    .Add(Projections.Property(() => secondReference.Year).WithAlias(() => secondReference.Year))))
                        .TransformUsing(Transformers.AliasToBean(typeof(FooBar)))
                        .List<FooBar>();

これが役立つことを願っています。

于 2011-08-10T20:08:03.217 に答える