我有一些记录
id title date
1 aaa 2005
2 bbb 2003
3 aaa 2007
4 ccc 2005
5 ccc 2009
我需要查询按日期检索记录
id title date
2 bbb 2003
3 aaa 2007
5 ccc 2009
查询:
select *
from Table
where Table.date = (SELECT Max(date)
FROM Table temp
WHERE temp.title = Document.title)
我有sql查询但需要Linq查询此操作
答案 0 :(得分:0)
这样的事情应该有效(C#):
var results =
from x in db.table
group x by x.title into g
select g.OrderByDescending(x => x.date)
.First();
现在要进行正确的分页,您必须在整个查询中调用Skip
/ Take
,如下所示:
var results =
(from x in db.table
group x by x.title into g
select g.OrderByDescending(x => x.date)
.First())
.Skip(0)
.Take(10);
答案 1 :(得分:0)
GroupBy
标题,按日期降序的订单组枚举,选择第一项。像这样的东西:
db.EntityName.GroupBy( en => en.title ).Select( g => g.OrderByDescending( en => en.date ).First() )