linq中的Noob,选择具有相同值的每条记录的第一条记录

Can*_*nez 4 linq linq-to-entities linq-to-sql

我尽我所能学习LINQ,但我仍然很难编码它.像这样,假设我有一个数据集或List可能,并且集合对象的名称或字段是列名.

Id | 日期|周一|周二|周三|周四|周五|周六|周日|计数

1 | 01/05 | = 1 = | == 1 == | == 1 = | == 1 = | 1 = | = 0 = | = 0 == | == 5 < - (1)

2 | 02/02 | = 1 = | == 1 == | == 1 = | == 1 = | 1 = | = 0 = | = 0 == | == 5**| - (2)

3 | 03/02 | = 1 = | == 1 == | == 1 = | == 1 = | 1 = | = 0 = | = 0 == | == 5**| - (2)

4 | 04/06 | = 1 = | == 1 == | == 1 = | == 1 = | 1 = | = 1 = | = 1 == | == 7 < - (1)

5 | 05/04 | = 1 = | == 1 == | == 1 = | == 1 = | 1 = | = 1 = | = 1 == | == 7**| - (3)

6 | 06/01 | = 1 = | == 1 == | == 1 = | == 1 = | 1 = | = 1 = | = 1 == | == 7**| - (3)

7 | 07/06 | = 1 = | == 1 == | == 1 = | == 1 = | 0 = | = 0 = | = 0 == | == 4 <----(1)

8 | 08/03 | = 1 = | == 1 == | == 1 = | == 1 = | 0 = | = 0 = | = 0 == | == 4**| - (4)

9 | 09/07 | = 1 = | == 1 == | == 1 = | == 1 = | 0 = | = 0 = | = 0 == | == 4**| - (4)

10 | 10/05 | 1 = | == 1 == | == 1 = | == 1 = | 0 = | = 0 = | = 0 == | == 4**| - (4)

我想要的只是获得所有数字(1)然后得到(2)因为它们属于第一个(1).接下来是(3)的组,因为它们属于第二个(1).最后一组(4)因为它们属于最后一个(1).

请帮忙.

- 重新提出问题.1.我怎样才能得到第一组5分,然后是7分组,最后是4分组?

Win*_*ith 5

您似乎想要按计数订购.

当你说"获得第一组5个"时你的意思是什么 - 你想得到什么数据?

澄清后更新

假设

public class Row
{
    public int ID{get;set;}
    public string Date{get;set;}
    public int Count{get;set;}
}

Row r1 = new Row{ID=1, Date="01/01/01", Count=5};
Row r2 = new Row{ID=2, Date="01/02/01", Count=5};
Row r3 = new Row{ID=3, Date="01/03/01", Count=5};
Row r4 = new Row{ID=4, Date="01/04/01", Count=7};
Row r5 = new Row{ID=5, Date="01/05/01", Count=7};
Row r6 = new Row{ID=6, Date="01/06/01", Count=7};
Row r7 = new Row{ID=7, Date="01/07/01", Count=4};
Row r8 = new Row{ID=8, Date="01/08/01", Count=4};
Row r9 = new Row{ID=9, Date="01/09/01", Count=4};
Row r10 = new Row{ID=10, Date="01/01/01", Count=4};

List<Row> rows = new List<Row>{r1,r2,r3,r4,r5,r6,r7,r8,r9,r10};
Run Code Online (Sandbox Code Playgroud)

然后

// We will assign results of our query to this variable
var result = 

// rows is a generic list of Row objects
rows                  

   // This splits the list into seperate categories organised by Count
   // After the GroupBy, we have an IEnumerable<IGrouping<Int32, Row>> - that is, a collection of collections of items sharing a common key (in this case Count)
   .GroupBy(r=>r.Count)  // r is of type Row

    // Now we are simply selecting the first item of each subgroup.
   .Select(g=>g.First()) // g is IGrouping<Int32,Row>, g.First() is of type Row
   ;
Run Code Online (Sandbox Code Playgroud)

   ID    Date        Count
    1    01/01/01    5
    4    01/04/01    7
    7    01/07/01    4
Run Code Online (Sandbox Code Playgroud)