Given this object:
public class MyObject
{
public string Sore { get; set; }
public int aye { get; set; }
}
With this data:
var ls=new List<MyObject>();
ls.Add(new UserQuery.MyObject(){Sore="A",aye=1});
ls.Add(new UserQuery.MyObject(){Sore="A",aye=2});
ls.Add(new UserQuery.MyObject(){Sore="A",aye=3});
ls.Add(new UserQuery.MyObject(){Sore="B",aye=1});
ls.Add(new UserQuery.MyObject(){Sore="B",aye=2});
You can do this quite easily:
var result=ls.GroupBy (l =>l.Sore)
.Select (l =>new
{
Root= l.Key,
Children=l.Select (x =>x.aye)
}
).ToList();
And then just compile the list:
foreach (var root in result)
{
foreach(var child in root.Children)
{
}
}
Arion source
share