2016-06-02 11 views
0

このネストされたグループの例はlinqで見つかりました。それにgroup byをもう1つ追加したいのですが、どのように見えますか?linqで3つのネストされたグループを行う方法

var queryNestedGroups = 
     from student in students 
     group student by student.Year into newGroup1 
     from newGroup2 in 
      (from student in newGroup1 
      group student by student.LastName) 
     group newGroup2 by newGroup1.Key; 

答えて

1

まず、サンプルクエリのいくつかの変数の名前を変更してみましょう:

var queryNestedGroups = 
    from e in source 
    group e by e.Key1 into g1 
    from e1 in 
     (from e in g1 
     group e by e.Key2) 
    group e1 by g1.Key; 

今、あなたは、同じパターンを使用して、別のネストされたグループレベルを追加することができます。

var queryNestedGroups = 
    from e in source 
    group e by e.Key1 into g1 
    from e1 in 
     (from e in g1 
     group e by e.Key2 into g2 
     from e2 in 
      (from e in g2 
      group e by e.Key3) 
     group e2 by g2.Key) 
    group e1 by g1.Key; 
関連する問題