2

列内のすべての一意のアイテムの Top(X) レコードを取得しようとしています。

例:

Inspections
InspectionId | RestaurauntName | InspectionDate
------------------------------------------------
1               Mom&Pop           10/10/12
2               SandwichesPlace   10/10/12
3               Mom&Pop           10/10/11
4               SandwichesPlace   10/10/11
5               Mom&Pop           10/10/10
6               SandwichesPlace   10/10/10
7               BurgerPlace       10/10/11
8               BurgerPlace       10/10/09
9               BurgerPlace       10/10/08

パラメータとして「2」の値を渡すと、レコード 1、2、3、4、7、8 が返されます。

このスニペットの例では、「restaurantName」ごとにいくつかのレコードを取得しようとしています。

    public IQueryable<Inspections> 
    GetLatestInspectionDatesForAllRestaurants(int numRecords) {

    IQueryable<Inspections> inspections= _session.Query<Inspections>()
                            .GroupBy(r=> r.RestaurauntName)
                            .SelectMany(g => g
                                            .OrderBy(r => r.InspectionDate)
                                            .Take(numRecords));

            return inspections;
     }

残念ながら、このエラーが発生しています。

      Query Source could not be identified:
      ItemName = g, ItemType = System.Linq.IGrouping`2

コードのこの時点で例外がトリガーされます

  var inspections = _inspectionRepository.GetLatestInspectionDatesForAllRestaurants(2).ToList(); 

私は何が欠けていますか?

編集

テストを実行した後、クエリが LINQ-To-Objects で正常に実行されることを確認しました。この分野で NHibernate.Linq がまだ不完全な可能性はありますか? 少し異なるクエリを実行したところ (以下)、実装されていない例外が発生しました。

       .GroupBy(r=> r.RestaurauntName)
                            .Select(g => g.First()).Take(5)

最後に Take(5) をドロップすると、次のようになります (これはサンプルであることを思い出してください。実際のケースでは、私の PK は Id です)。

  {"Column '.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."}

create基準などの使い方を学ぶ必要がありますか?

4

2 に答える 2

0
public class Inspections{
    public int InspectionId {get;set;}
    public string RestaurantName {get;set;}
    public DateTime InspectionDate {get;set;}
}
void Main()
{
    var result=GetLatestInspectionDatesForAllRestaurants(2);
    result.Dump();
}
public IQueryable<Inspections> 
    GetLatestInspectionDatesForAllRestaurants(int numRecords) {
        var i = new[]{
        new Inspections{InspectionId=1,RestaurantName="Mom&Pop",InspectionDate=DateTime.Parse("10/10/12")},
        new Inspections{InspectionId=2,RestaurantName="SandwichesPlace",InspectionDate=DateTime.Parse("10/10/12")},
        new Inspections{InspectionId=3,RestaurantName="Mom&Pop",InspectionDate=DateTime.Parse("10/10/11")},
        new Inspections{InspectionId=4,RestaurantName="SandwichesPlace",InspectionDate=DateTime.Parse("10/10/11")},
        new Inspections{InspectionId=5,RestaurantName="Mom&Pop",InspectionDate=DateTime.Parse("10/10/10")},
        new Inspections{InspectionId=6,RestaurantName="SandwichesPlace",InspectionDate=DateTime.Parse("10/10/10")},
        new Inspections{InspectionId=7,RestaurantName="BurgerPlace",InspectionDate=DateTime.Parse("10/10/11")},
        new Inspections{InspectionId=8,RestaurantName="BurgerPlace",InspectionDate=DateTime.Parse("10/10/09")},
        new Inspections{InspectionId=9,RestaurantName="BurgerPlace",InspectionDate=DateTime.Parse("10/10/08")}
    };
    var result=i.GroupBy(g=>g.RestaurantName).SelectMany(g=>g.OrderByDescending(d=>d.InspectionDate).Take(2));
    return result.AsQueryable();
    }

これは問題なく実行されるので、元のクエリ可能オブジェクトの有効期間に問題があると想定しています。次のことを試してください。

public IQueryable<Inspections> 
  GetLatestInspectionDatesForAllRestaurants(int numRecords)
{

  IQueryable<Inspections> inspections= _session.Query<Inspections>()
                        .GroupBy(r=> r.RestaurauntName)
                        .SelectMany(g => g
                                        .OrderByDescending(r => r.InspectionDate)
                                        .Take(numRecords));

  return inspections.ToList().AsQueryable();
}

これはパフォーマンスが悪いが、式から NHibernate を排除する必要がある 3 番目のオプションです。

public IQueryable<Inspections> 
  GetLatestInspectionDatesForAllRestaurants(int numRecords)
{

  IQueryable<Inspections> inspections= _session.Query<Inspections>().ToList()
                        .GroupBy(r=> r.RestaurauntName)
                        .SelectMany(g => g
                                        .OrderByDescending(r => r.InspectionDate)
                                        .Take(numRecords));

  return inspections.ToList().AsQueryable();
}
于 2013-06-14T21:51:42.173 に答える