Is it possible to use GroupBy in LINQ using the collection property?
eg.
void Main() { var t1 = new Test() { Children = new List<string>() { "one", "two" } }; var t2 = new Test() { Children = new List<string>() { "one", "two" } }; var t3 = new Test() { Children = new List<string>() { "one", "three" } }; var tests = new List<Test>() { t1, t2, t3 }; var anon = from t in tests select new { Children = t.Children }; anon.GroupBy(t => t.Children).Dump(); } public class Test { public List<string> Children {get;set;} }
In this example, I would like to hope for two groups:
Key: List () {"one", "two"} Value: t1, t2
Key: List () {"one", "three"} Value: t3
I understand that anonymous types are not compared by reference, but by matching equality with their public properties.
However, the actual result consists of three groups:
Key: List () {"one", "two"} Value: t1
Key: List () {"one", "two"} Value: t2
Key: List () {"one", "three"} Value: t3
If this is not possible, is there a way to get the result I want?
Hope this clearly explained ...
Thenextman
source share