LINQ分组排序后获取每组第一条记录
目前有一张数据表Student,包含以下字段:
CREATE TABLE [dbo].[Student](
[Sno] nchar NOT NULL,
[Sname] nchar NOT NULL,
[Sex] nchar NULL,
[Sage] [tinyint] NULL,
[Sdept] nchar NULL,
[Sdate] [datetime],
) ON [PRIMARY]
按照Sno和Sname进行分组,在按时间进行倒序,我们需要获得Student表的完整信息
var q = from n in Student
group n by new {n.Sno,n.Sname} into g
select g.OrderByDescending(t=>t.Sdate).FirstOrDefault();
或者
var q = from n in Student
group n by new {n.Sno,n.Sname} into g
select g;
再进行循环
List
foreach (var item in query)
{
var itList = item.OrderByDescending(x => x.Sdate).FirstOrDefault();
list.Add(ObjectMapper.Map<StudentListDto>(itList));
}
var salesOrderEntryCount = list.Count();
var salesOrderEntryDtos = list.Skip(input.SkipCount).Take(input.MaxResultCount);