2009-05-01 43 views
4

我尝试我最好的学习LINQ,但进出口仍然有一个很难对其进行编码。就像这样,假设我有一个数据集或一个List,并且集合对象的名称或字段是列名称。菜鸟在LINQ,选择每个记录的第一个记录具有相同值

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组?

+0

怎么办(1)的和(2)' s(原文如此)属于第一个(1)。我无法理解这个问题。 – spender 2009-05-01 11:56:13

+0

你的问题不清楚。请解释您的数据是如何构建的,以便一组行“属于”另一组。 – 2009-05-01 12:10:44

回答

5

看起来你想要按点数排序。

当你说“得到第一批5的”你是什么意思 - 你想获得什么样的数据?

UPDATE澄清

假设

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}; 

后接着

// 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 
    ; 

给出

ID Date  Count 
    1 01/01/01 5 
    4 01/04/01 7 
    7 01/07/01 4 
相关问题