0

我有一些记录

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 查询

4

2 回答 2

0

Something like this should work (C#):

var results = 
    from x in db.table
    group x by x.title into g
    select g.OrderByDescending(x => x.date)
            .First();

Now for proper paging you'd have to call Skip / Take on the entire query like this:

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);
于 2013-10-04T20:21:23.067 回答
0

GroupBy标题,按日期降序排列的订单组枚举,选择第一项。像这样的东西:

db.EntityName.GroupBy( en => en.title ).Select( g => g.OrderByDescending( en => en.date ).First() )
于 2013-10-04T20:22:48.790 回答