If i have 3 columns in a DataTable with following data:
1 A qwqw
2 A wewe
3 B tyty
4 B trtr
5 C iuiu
What is the easiest and efficient way in which i can put the above data into a Dictionary(Of String, List(Of Foo)) like this:
[A] -> {New Foo(1, A, qwqw), New Foo(2, A, wewe)}
[B] -> {New Foo(3, B, tyty), New Foo(4, B, trtr)}
[C] -> {New Foo(5, C, iuiu)}
where Foo is a type with members that represent the fields in the table.
What i have done is written a query(LINQ) to first get the unique values in the 2nd columns in a List(Of String). Then iterated over this list to fire another query for each element in the list to get the corresponding rows in a list out of which objects of Foo are created and put in a list which is then added to my Dictionary as value with the key as the element. Any ideas?
Here is a C# answer:
The idea is to first group by the middle column (#1), and then convert groups to dictionary, using group key as the dictionary key, and creating
Foofrom each row in the group.