3

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

user1590636
  • 1,174
  • 6
  • 26
  • 56

1 Answers1

1

you may want to use System.Linq.Dynamic

var dt = new DataTable();
var res = new List<DataTable>();

dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Country", typeof(string));
dt.Columns.Add("Supplier", typeof(string));
dt.Rows.Add(515, "DE", "A");
dt.Rows.Add(515, "CH", "A");  
dt.Rows.Add(515, "FR", "A");
dt.Rows.Add(516, "DE", "B");
dt.Rows.Add(516, "FR", "B");
dt.Rows.Add(517, "DE", "C");
dt.Rows.Add(517, "IT", "C");

var fields = new List<string>() { "Supplier", "Country"};
var qfields = string.Join(", ", fields.Select(x => "it[\"" + x + "\"] as " + x));
// qfields = "it[\"Supplier\"] as Supplier, it[\"Country\"] as Country"

var q = dt
    .AsEnumerable()
    .AsQueryable()
    .GroupBy("new(" + qfields + ")", "it")
    .Select("new (it as Data)");
foreach (dynamic d in q)
{
    var dtemp = dt.Clone();

    foreach (var row in d.Data)
        dtemp.Rows.Add(row.ItemArray);

    res.Add(dtemp);
}
Roman Pekar
  • 107,110
  • 28
  • 195
  • 197