Linq分组后,再对分组后的每组数据进行排序,获取每组的第一条记录

Linq分组后,再对分组后的每组数据进行排序,获取每组的第一条记录

Linq to SQL

var result = from transRecord in TransRecord
        group transRecord by transRecord.TransType into g
        select g.OrderByDescending(o=>o.Date)
        .FirstOrDefault();

Linq to Entity

var result = _dbContext
             .TransRecord
             .GroupBy(g => g.TransType)
             .Select(s => s.OrderByDescending(o => o.Date)
             .FirstOrDefault());

 

posted on 2021-09-02 10:08  大豆男生  阅读(461)  评论(0编辑  收藏  举报

导航