Linq to datatable conversion

patrick

Well-known member
Joined
Dec 5, 2021
Messages
263
Programming Experience
1-3
Hello.

Datatable dt = from b in datatable.AsEnmerable() group b by b.Field<string>("column");

In above code, an errr ocurs.
 
What is the error?
 
What is the error?
Error CS0266 : Cannot implicitly convert type
System.collections.Generic.IEnumerable<System.Linq.IGrouping<string, System.Data.DataRow>>' to System.Data.Datatable.


I want to Return Datatable type, By Grouping Colum of Datatable.

The method of adding System.Data.DatasetExtention is Excluded.
 
Last edited:
The result of that query is not a DataTable so it can't be assigned to a variable of that type. If you want a DataTable, you have to create one. LINQ to DataSet does provide a CopyToDataTable method but that's not going to work in your case anyway, because you have performed grouping in your query so the result does not contain DataRows. If you want a new DataTable with a different schema to the old one, you're going to have to create it manually. If you're still confused, maybe you should provide more detail about your current input and your expected output.
 
Or he could do a SelectMany() to try to flatten the grouping back down to data rows, and then use the CopyToDataTable().
 
Or he could do a SelectMany() to try to flatten the grouping back down to data rows, and then use the CopyToDataTable().
True, but that seems like it would probably defeat the purpose of the grouping in the first place. I'm not sure that we have enough information to tell for sure either way. Mind you, the OP did say this:
The method of adding System.Data.DatasetExtention is Excluded.
so I'm not sure whether that means that it's a class assignment and they're not allowed to use CopyToDataTable anyway.
 
Or he could do a SelectMany() to try to flatten the grouping back down to data rows, and then use the CopyToDataTable().

Datatable dt = from b in datatable.AsEnmerable() group b by b.Field<string>("column");

the
Or he could do a SelectMany() to try to flatten the grouping back down to data rows, and then use the CopyToDataTable().

I was here to do this.
 
so I'm not sure whether that means that it's a class assignment and they're not allowed to use CopyToDataTable anyway.
Given that the OP is @patrick , he or she (since they seem to be the body double of @Anwind) always seems to have some odd arbitrary inexplicable restriction, that would not be surprising.
 
Please help me ㅠㅠ

It's difficult to help some people; instead of responding to questions they are asked, they just carry on repeating that they need help. This part we already know, given that they are asking questions in a help forum.

Make yourself easier to help for best effectiveness
 
Back
Top Bottom