C# 如何使用Linq获取每个组中的第一条记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19012986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:54:30  来源:igfitidea点击:

How to get first record in each group using Linq

c#linqc#-4.0linq-to-entities

提问by Arian

Considering the following records:

考虑以下记录:

   Id          F1            F2             F3 
 -------------------------------------------------
   1           Nima          1990           10
   2           Nima          1990           11
   3           Nima          2000           12
   4           John          2001           1
   5           John          2002           2 
   6           Sara          2010           4

I want to group by based on the F1field and sort by Idand get all fields from the first record of group similar to these records:

我想根据F1字段进行分组并排序Id并从类似于这些记录的组的第一条记录中获取所有字段:

   Id          F1            F2             F3 
 -------------------------------------------------
   1           Nima          1990           10
   4           John          2001           1
   6           Sara          2010           4

How can I do this using linq?

如何使用 linq 执行此操作?

采纳答案by Alireza

    var res = from element in list
              group element by element.F1
                  into groups
                  select groups.OrderBy(p => p.F2).First();

回答by King King

var result = input.GroupBy(x=>x.F1,(key,g)=>g.OrderBy(e=>e.F2).First());

回答by Stavros Koureas

The awnser of @Alireza is totally correct, but you must notice that when using this code

@Alireza 的 awnser 是完全正确的,但是您在使用此代码时必须注意

var res = from element in list
          group element by element.F1
              into groups
              select groups.OrderBy(p => p.F2).First();

which is simillar to this code because you ordering the list and then do the grouping so you are getting the first row of groups

这与此代码类似,因为您对列表进行排序,然后进行分组,因此您将获得第一行组

var res = (from element in list)
          .OrderBy(x => x.F2)
          .GroupBy(x => x.F1)
          .Select()

Now if you want to do something more complex like take the same grouping result but take the first element of F2 and the last element of F3 or something more custom you can do it by studing the code bellow

现在,如果你想做一些更复杂的事情,比如采用相同的分组结果,但采用 F2 的第一个元素和 F3 的最后一个元素或者更自定义的东西,你可以通过研究下面的代码来完成

 var res = (from element in list)
          .GroupBy(x => x.F1)
          .Select(y => new
           {
             F1 = y.FirstOrDefault().F1;
             F2 = y.First().F2;
             F3 = y.Last().F3;
           });

So you will get something like

所以你会得到类似的东西

   F1            F2             F3 
 -----------------------------------
   Nima          1990           12
   John          2001           2
   Sara          2010           4

回答by Rubens Mussi Cury

Use it to achieve what you want. Then decide which properties you want to return.

用它来实现你想要的。然后决定要返回哪些属性。

yourList.OrderBy(l => l.Id).GroupBy(l => new { GroupName = l.F1}).Select(r => r.Key.GroupName)