Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a list with two columns (ID,Value).

I want grouped list with value columns then select greatest Id of it.

ID | Value

1 | 101

2 | 305

3 | 102

4 | 101

5 | 305

6 | 101

Result :

ID | Value

3 | 102

5 | 305

6 | 101

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

To just show the list you can use a view with "Group By" = "Value" and "Total" : "ID" - Maximum

But if you need this for any data processing purpose or customized showing then LINQ is best option I guess, you can use CAML Query group by but that will be no use for your purpose, so i think something like this is your best option :

DataTable dtList = list.Items.GetDataTable();
var grouped = from row in dtList.AsEnumerable()
              group row by row["Value"] into valueGroup
              select new { Value = valueGroup.Key, ID = valueGroup.Max(id => id["ID"]) };

just wanted to point one very common fact: when you use ID as a column name it will have different internal name, so id["ID"] will be different

share|improve this answer
    
Thanks a lot, but how to convert linq to datatable? –  ar.gorgin Aug 18 '12 at 8:09
    
in many ways,as here anonymous type is involved so copyToDataTable wont work,you may consider this MSDN.But I always use the simplest way : DataTable dtGrouped=new DataTable();dtGrouped.Columns.Add("Value", typeof(int));dtGrouped.Columns.Add("ID", typeof(int)); foreach (var group in grouped) { DataRow row = dtGrouped.NewRow(); row["Value"] = group.Value; row["ID"] = group.ID; dtGrouped.Rows.Add(row); } –  Diptarag Aug 18 '12 at 9:12
    
Thanks a lot :) –  ar.gorgin Aug 18 '12 at 9:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.