![](/img/trans.png)
[英]LINQ - GroupBy a key and then put each grouped item into separate 'buckets'
[英]Take Top(X) of each grouped item in LINQ
我正在尝试为列中的每个唯一项都选择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."}
我需要学习使用创建标准或其他条件吗?
好吧,我得到了我需要生成的SQL,它花了一些时间进行研究。 我确信我最初的问题与LINQ和NHibernate有关,尽管罗伯特发表的最后一个查询在技术上是正确的,可以解决。
由于IQueryable的性质,直到必要时才枚举,这将生成一个SQL脚本。 SQL本身会生成“ Where Exists”子句。
public IQueryable<Inspections>
GetLatestInspectionDatesForAllRestaurants(int numRecords)
{
var subQuery = _session.Query<Inspections>()
.OrderByDescending(x => x.InspectionDate);
var inspections = _session.Query<Inspections>()
.Where(x => subQuery.Where(y => y.InspectionDate ==
x.InspectionDate).Take(numRecords).Contains(x))
.OrderBy(x => x.WellDataId);
return inspections;
}
在那里,希望它最终可以帮助其他人。
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:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.