My problems are so hard to explain.
So, look at my code below.
I have a class model as follows:
public class MyModel{
public int ID{get;set;}
public List<string> CHILD{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
I have a list of MyModel, and some of elements have same ID.
How can I create a new list from list of MyModel with unique ID and CHILD has combined?
Thank you so much
var models = new[] {
(id: 1, child: new[] { 1, 2 }),
(id: 2, child: new[] { 3, 4 }),
(id: 1, child: new[] { 5, 6, 7 }),
(id: 3, child: new[] { 8 }),
(id: 2, child: new[] { 9, 0 }),
};
var res = models.GroupBy(m => m.id)
.Select(g => (id: g.Key, child: g.SelectMany(m => m.child)));
foreach (var m in res)
{
Console.WriteLine(m.id + ": " + string.Join(", ", m.child));
}
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
output: