I have a DataTable, and i want to grouped with a column .
I use this code for it :
DataTable dtList = list1.Items.GetDataTable();
var grouped = from row in dtList.AsEnumerable()
group row by row["T"] into valueGroup
select new { Value = valueGroup.Key, ID = valueGroup.Max(id => id["ID"]) };
My problem is : it is select two column(id,value). I want select multiply column.
How to select other column of dtlist ?
If you want to select multiple columns to be groupped by, try something like this:
This will give you multiple columns returned as an anonymous type objects with
TandUproperties accessible in theValuefield of the return set.I’ve tested this and it seems to work on a sample data set.
It’s based on a solution described here: http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/54f6ac70-e74c-4d9d-a9ee-6373714c0755.
Edit
When using
group byclause all items are divided into groups and for each group only one item is returned, so in order to refer to a column, you have to either put it in thegroup byclause or use an aggregation method on it (like you did withIDfield andMax()method).However, if you want to make the groups, get the maximum
IDfor each group and then retrieve other columns for those items (with maximumIDvalue in each group) you can use ajoinwith the same table. This code shows how to do it in your case for sampleTandUcolumns. It seems to work correctly on a test data set:If you need any help with this, let me know. If it does not return data you want to retrieve, please give some more details about it.