Split a DataTable into 2 or more DataTables based on Column value

后端 未结 2 625
执笔经年
执笔经年 2020-12-06 05:27

I have a DataTable called \"DTHead\" which has the following records,

 MIVID      Quantity         Value
------     ----------       --------
   1                    


        
相关标签:
2条回答
  • 2020-12-06 06:20

    Use LINQ to DataTable to group the first column by GroupBy, and use method CopyToDataTable to copy list of rows to DataTable

     List<DataTable> result = DTHead.AsEnumerable()
                .GroupBy(row => row.Field<int>("MIVID"))
                .Select(g => g.CopyToDataTable())
                .ToList();
    

    Then you can get the result as a list of DataTables as you expected.

    0 讨论(0)
  • 2020-12-06 06:24
    DataTable tbl = new DataTable("Data").AsEnumerable()
        .Where(r => r.Field<int>("ParentId") == 1) // ParentId == 1
        .Where(r => r.Field<int>("Id") > 3) // Id > 3
        .Where(r => r.Field<string>("Name").Contains("L")) // Name contains L
        .OrderBy(r => r.Field<int>("Id")) // Order by Id
        .CopyToDataTable();
    
    0 讨论(0)
提交回复
热议问题