How to convert IEnumerable<IGrouping<int, DataRow>> to DataTable?

1.9k Views Asked by At

C#.net, .Net Framework 4.5, VS 2015 I get DataTable as input for the method. Need to sort in specific way (below): by first element in groups of two. And then must to return sorting results as DataTable.

I found how to sort incoming DataTable rows, but how to convert sorted records to DataTable ?

public DataTable sort(DataTable, dt)
{
    DataTable dt;

    var v1 = dt.Select()
         .OrderBy(r => r["TeamId"])
         .ThenBy(n => n["Last Name"])
         .ThenBy(n => n["First Name"])
         .GroupBy(x => index++ / 2)
         .OrderBy(row => row.First()["Last Name"]);

    // need to convert v1 to DataTable sorted_dt  - how ?
    // v1 is type of  IOrderedEnumerable<IGrouping<int, DataRow>>

    return sorted_dt;
 }

Many thanks

1

There are 1 best solutions below

0
Karen Slon On

I just found that following works:

sorted_dt = new DataTable(); sorted_dt = v1.SelectMany(g => g).ToArray().CopyToDataTable();