2009-05-01 44 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 
相關問題