Linq笔记_GourpBy

2018/04/27 DotNetCore 共 774 字,约 3 分钟

工作内容的临时备忘,Linq代码片段。

分组查询取每组中最小的值或最大值

参考:https://stackoverflow.com/questions/1704821/linq-to-sql-groupby-and-max-to-get-the-object-with-latest-date

var bridgeCongnizaceAssessSource =await _bridgeCognizanceAssessRepository
                .Where(x => x.IsDeleted == 0 && x.TableName == BridgeServiceConstValue.Table_thematic_evaluation)
                .GroupBy(d => d.ComeFromId).Select(grp=>new DangerousBridgeCognizanceAssessEntity(){ComeFromId = grp.Key, DangerousBridgeLv = grp.Min(x=>x.DangerousBridgeLv)}).ToListAsync();

            
  var bridgeMainLineIdList = bgeLineInfoRepository.GroupBy(d => d.Bge_Id).Select(grp => grp.Min(x => x.Id))
                .ToListAsync();

分组取最小值。

image-20211229005439581

image-20211229005819943

参考

image-20211229011525603

image-20211229012041294

参考资料

https://www.pluralsight.com/guides/grouping-aggregating-data-linq

linq比较两个list是否相等

image-20220518180210367image-20220518185403773

参考:https://www.techiedelight.com/compare-two-lists-for-equality-csharp/

文档信息

Search

    Table of Contents