FluentNhibernate:查询以检索不同的值

FluentNhibernate: Query to retrieve distinct values

在 DB2 LUW 9.7 数据库上使用 FluentNhibernate 1.3.0.0、NHibernate 3.3.1.4000。

我只想从一个 table / 实体中获取一些不同的数据。在 SQL 中,这很容易:

select distinct Corporation, CalculationDate, ValuationRule
from MySchema.MyTable
where State == 0

现在,我正在尝试使用 Linq 获取这些数据,但它不起作用...

首先尝试使用 select:

var result = Session.Query<MyEntity>()
        .Where( x => x.State == State.Pending)
        .Select(
           x =>
              new
              {
                 Corporation = x.Corporation,
                 CalculationDate = x.CalculationDate,
                 ValuationRule = x.ValuationRule,
              }).Distinct().ToList();

产生异常:此 SelectClauseVisitor 不支持表达式类型 'NhDistinctExpression'。

第二次尝试,使用 Groupby 并仅获取键:

var result = Session.Query<MyEntity>()
        .Where( x => x.State == State.Pending)
        .GroupBy(
           x =>
              new
              {
                 Corporation = x.Corporation,
                 CalculationDate = x.CalculationDate,
                 ValuationRule = x.ValuationRule,
              }).Select( x => x.Key).ToList();

产生异常:"Could not execute Query"。抱怨在 select 术语中声明的 group by 子句中缺少另一个字段 "Model"。这让我很困惑,因为指定字段存在于 table 中,但我不想在该用例中使用该字段...

我缺少什么?

尝试使用 QueryOver...

var result = Session.QueryOver<MyEntity>()
    .Where(x => x.State == State.Pending)
    .SelectList(list => list
        .SelectGroup(x => x.Corporation)
        .SelectGroup(x => x.CalculationDate)
        .SelectGroup(x => x.ValuationRule)               
    )
    .ToList();

如果你想使用不同的:

var result = Session.QueryOver<MyEntity>()
    .Where(x => x.State == State.Pending)
    .SelectList(list => list
                .Select(Projections.Distinct(Projections.Property<MyEntity>(x => x.Corporation)))
                .Select(x => x.CalculationDate)
                .Select(x => x.ValuationRule)
                 )
                .ToList();

在 Brenda 的两个示例中都缺少转换。

免责声明:首先检查 DTO 或 Linq 投影中的类型是否正确。

public class MyDto
{
    public string Corporation { get; set; }
    public DateTime? CalculationDate { get; set; }
    public string ValuationRule { get; set; }
}

MyDto myDto = null;

var result = Session.QueryOver<MyEntity>()
    .Where(x => x.State == State.Pending)
    .SelectList(list => list
        .Select(Projections.Distinct(Projections.Property<MyEntity>(x => x.Corporation))).WithAlias(() => myDto.Corporation)
        .Select(x => x.CalculationDate).WithAlias(() => myDto.CalculationDate)
        .Select(x => x.ValuationRule).WithAlias(() => myDto.ValuationRule)
        )
    .TransformUsing(Transformers.AliasToBean<MyDto>())
    //.TransformUsing(Transformers.AliasToBean<MyEntity>()) // You can use your entity but i recommend to use a DTO (Use MyEntity in the alias too)
    .ToList();

如果您不想使用转换器,则需要转换为 obj 数组:

var result = Session.QueryOver<MyEntity>()
    .Where(x => x.State == State.Pending)
    .SelectList(list => list
        .Select(Projections.Distinct(Projections.Property<MyEntity>(x => x.Corporation)))
        .Select(x => x.CalculationDate)
        .Select(x => x.ValuationRule)
        )
    .ToList<object[]>()
    //.Select(x => new    // This is optional to use anonymus type instead a object[]
    //      {
    //         Corporation = (string) x[0],
    //         CalculationDate = (DateTime?) x[1],
    //         ValuationRule = (string) x[2]
    //      })
    //.List()
    ;