Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have list which is grouped by EmailID column from one dataset. Below is my query for grouping
C#
List<DataTable> result = dsgetTimesheetlist.AsEnumerable()
                           .GroupBy(row => row.Field<string>("TeamLeaderEmailID"))
                           .Select(g => g.CopyToDataTable())
                           .ToList();


I am getting result which i expected. But Now I want to split those groups into Many datatables. Like, one group of email id to one datatable, another group of emailID to another datatable.
Posted

1 solution

One way would be that you don't cast the results as a list. Remove the ToList call from the end and let the result be a data tables.

After that using Distinct for they key column fetch all the distinct values and lop through them. In each iteration create a new data table based on a dataview.

So in pseudo code something like (not to be compiled):
C#
foreach (string email in result.select(x => x.Field("email")).Distinct()) {
  result.DefaultView.RowFilter = "email = '" + email + "'");
  datatablelist.Add(result.DefaultView.ToTable());
}

As a side-note if you split the data into multiple data tables you loose the ability to easily query and manipulate all rows together. Because of this splitting the data into parts is useful only in quite rare situations. If the reason is showing smaller portions of data in UI you can always use filters for that.

Links:
- DataView.RowFilter[^]
- DataView.ToTable[^]
 
Share this answer
 
v3
Comments
Member 11719500 22-Aug-15 4:10am    
giving error if i remove tolist
Wendelius 22-Aug-15 4:15am    
The type of result needs to be changed correspondingly. Try using
var result = ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900