我有一个关键字列表;
我想将它们分组(前5位,注意foo不在列表中):
所以基本上:在创建日期之后列出关键字,按关键字和.Take(5)分组。那怎么可能使用lambda表达式?以及如何将它们作为特定类型的对象返回?我们说类型LatestSearch
。
更新 结束了这个查询:
var searches = db.Searches
.GroupBy(s => s.Keyword)
.Select(g => g.OrderByDescending(o => o.CreatedDate).First())
.OrderByDescending(o => o.CreatedDate)
.Take(5);
答案 0 :(得分:2)
像这样:
table.Where(s => whatever)
.GroupBy(s => s.Keyword)
.Select(g => new {
Keyword = g.OrderByDescending(s => s.Date),
Count = g.Count(),
Date = g.Max(s => s.Date())
})
.OrderByDescending(s => s.Count)
.Take(5);
编辑:如果您只想要IEnumerable<LatestSearch>
,请写下以下内容:
table.Where(s => whatever)
.GroupBy(s => s.Keyword)
.OrderByDescending(g => g.Count())
.Select(g => g.First());
.Take(5);