2009-05-01 7 views
4

私はLINQを学ぶのに最善を尽くしていますが、まだそれをコーディングするのは苦労しています。これと同じように、データセットまたはListがあり、コレクションオブジェクトの名前またはフィールドが列名であるとします。Noobは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)

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

6 | 06/01 | = 1 = | == 1 == | == 1 = | == 1 = | (3)

7 | = 1 == | 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)のグループは2番目のグループ(1)に属しているからです。最後の(4)のグループは最後のもの(1)に属しているからです。

助けてください。

- 質問は言い換えられます。 1.私は5の最初のグループを取得し、次に7のグループを、最後に4のグループを取得できますか?

+0

行う方法(1)の(2) '与えますs(sic)は最初の(1)に属します。私はこの質問をあまり理解できません。 – spender

+0

あなたの質問は明確ではありません。 1組の行が別の行に属するようにデータがどのように構成されているかを説明してください。 –

+0

@spenderとjoe、 私の不明な質問は申し訳ありません。私はちょうどそれを改めた。とにかく、私はちょうど5、7と4の3つのグループを取得したい。 – grayman

答えて

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 
+0

@joe、その行全体です(データセットの場合)。 – grayman

+0

また、オブジェクト全体がリストのメンバーである場合 grayman

+0

申し訳ありませんが、まだ分かりません。同じカウントを持つ複数の行があるので、行全体はどういう意味ですか?期待している出力の例を追加できますか? –

関連する問題