2016-12-22 3 views
1

G'dayの人々。このようなサブリストに基づいてIGroupingを作成する

public class ObjectA 
    { 
     public string PropertyA { get; set; } 
     public List<string> ListProperty { get; set; } 
    } 

とコレクションクラス:

public class CollectionB 
    { 
     public List<ObjectA> Collection { get; set; } 
    } 

thusly使用されている。一つは作成しますどのように

public void MethodC(CollectionB coll) 
    { 
     coll.Add(new ObjectA 
     { 
      PropertyA = "merry" 
      ListProperty = new List<string> { "foo", "bar" } 
     }); 
     coll.Add(new ObjectA 
     { 
      PropertyA = "xmas" 
      ListProperty = new List<string> { "yee", "har", "foo" } 
     }); 
     coll.Add(new ObjectA 
     { 
      PropertyA = "folks" 
      ListProperty = new List<string> { "bar", "bah", "humbug" } 
     }); 
    } 

はちょうど私は、次のクラスを持っていたとしましょうCollectionBのIGrouping<string, ObjectA>はObjectAのPropertyListプロパティstキー("foo""bar"など)としてのリング?

グループに重複しても私を悩ましくはしません。

私はクエリ構文に対する選好持っているこれらのセットアップについて

答えて

1

from a in coll from p in a.ListProperty group a by p 

サイドノート:上記のリストを取得するにはIEnumerable<IGrouping<string, ObjectA>>を作成しますが、単に(from a in coll from p in a.ListProperty group a by p).ToList()

+0

あなたは知っています、今、本当のピエロの靴のように感じます。 私はクエリ構文の使用を考慮しませんでした。これは主に、それを使用する必要があっても約8年経っているためです。 今や明らかになったようです;) 乾杯してください。 – Ubermonk

2
list.SelectMany(t => t.ListProperty.Select(q => new { ObjectA = t, Prop = q })).GroupBy(t => t.Prop, w=>w.ObjectA) 
+0

乾杯を使用していますが、私は@ Me.Nameの答えは多分もっと簡潔だと思います。私は間違いなくあなたのものを書いておきます。もしもの場合のために、将来の使用のためにあなたのものを書き留めておきます。 – Ubermonk

関連する問題