I'm trying to loop through data to build a list of objects that each has a list of objects. Here's what the data looks like
AccountID AccountName ListItem
------------------------------------
1 Foo Item1
1 Foo Item2
2 Test Value1
2 Test Value2
2 Test Value3
3 Bar List1
3 Bar List2
3 Bar List3
The other posts I've seen didn't quite tackle my issue. From my data, I would expect to create 3 Account objects, each having a List<string> of the ListItem data.
I was hoping to do something like this:
var accountGroups = myDataTable.GroupBy(x=>x.Field<string>("AccountName"));
and then loop through accountGroups to add the ListItems, but of course DataTable doesn't implement IEnumerable<T>
Is this possible with LINQ or should I just write the loop manually?
myDataTable.Rows.GroupBy(..)ormyDataTable.AsEnumerable().GroupBy(..)