LINQ分組排序后獲取每組第一條記錄


當前有一張數據表{Student},包含了如下的字段信息:
CREATE TABLE [dbo].[Student](
[Sno] [nchar](7) NOT NULL,
[Sname] [nchar](10) NOT NULL,
[Sex] [nchar](2) NULL,
[Sage] [tinyint] NULL,
[Sdept] [nchar](20) NULL,
    [Sdate] datetime NULL,
) ON [PRIMARY]
我們需要獲得學生的完整信息
方案一
    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<StudentListDto> list = new List<StudentListDto>();
            foreach (var item in query)
            {
                var itList =  item.OrderByDescending(x => x.Sdate).FirstOrDefault();

                list.Add(ObjectMapper.Map<StudentListDto>(itList));
            }
最后進行其他事情


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM