I have a datatable that looks lik the following
ID Country Supplier
515 DE A
515 CH A
515 FR A
516 DE B
516 FR B
517 DE C
517 IT C
and i have a List<string>
that contins a dynamic number of column names, for example if the list contained a single column:
Supplier
i want to produce a List<DataTable>
or a DataSet from this table and seperate the table based on the column names in that list so in this case i seperate only by the Supplier
column, the result will be 3 DataTables that look like the following
----------table 1--------
515 DE A
515 CH A
515 FR A
----------table 2--------
516 DE B
516 FR B
----------table 3--------
517 DE C
517 IT C
but if the List<string>
of column names contained for example the following:
Supplier
Country
the the result would be 7 Datatables each datatable containing a row
----------table 1--------
515 DE A
----------table 2--------
515 CH A
----------table 3--------
515 FR A
----------table 4--------
516 DE B
----------table 5--------
516 FR B
----------table 6--------
517 DE C
----------table 7--------
517 IT C
another example is, if the List<string>
of column names contained only the Country
column then the result would be
----------table 1--------
515 DE A
516 DE B
517 DE C
----------table 2--------
515 CH A
----------table 3--------
515 FR A
516 FR B
----------table 4--------
517 IT C
how can i achive this using linq, the query will be dynamic based on the column names that are contained in the list, could you please guide me?
i have done it already using a daynamic string for DataTable.Select and Select distinct and nested loops but it looks complicated and i wonder if there is more efficient way to achive this