簡體   English   中英

FluentNhibernate:查詢以檢索不同的值

[英]FluentNhibernate: Query to retrieve distinct values

在DB2 LUW 9.7數據庫上使用FluentNhibernate 1.3.0.0,NHibernate 3.3.1.4000。

我想從一個表/實體中獲取一些不同的數據。 在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();

結果異常:“無法執行查詢”。 抱怨選擇術語中所述的group by子句中缺少的另一個字段“Model”。 這讓我很困惑,因為表中存在指定的字段,但我不想在該用例中使用該字段...

我錯過了什么?

嘗試使用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();

如果你想使用distinct:

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();

在布倫達的兩個例子中都缺少轉型。

免責聲明:首先檢查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()
    ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM