本文介绍了Linq仅针对特定属性的独特方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码段中,如何在选择所有其他值[ e.Firstname, e.Surname, e.EntityNumber ]时仅对e.EntityNumber使用独特的功能.

In this code snippet how do I use distinct feature only for e.EntityNumber while selecting all the other values [ e.Firstname, e.Surname, e.EntityNumber ].

response
   .Categories
   .SelectMany(c => c.Events)
   .Select(e => new { e.Firstname, e.Surname, e.EntityNumber })
   .Distinct()
   .ToList()

推荐答案

您可以改用GroupBy:

response
   .Categories
   .SelectMany(c => c.Events)
   .Select(e => new { e.Firstname, e.Surname, e.EntityNumber })
   .GroupBy(x => x.EntityNumber)
   .Select(grp => grp.First())
   .ToList()

我已经选择了每个组的任意行(第一个).您可以根据自己的逻辑更改grp.First().

I have selected an arbitrary row of each group (the first). You can change grp.First() according to your logic.

//...
.GroupBy(x => x.EntityNumber)
.Select(grp => grp.OrderBy(x => /* Insert logic here */).First())
//...

这篇关于Linq仅针对特定属性的独特方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 01:10