Sharepoint - How to use GroupBy in CAML query

For group by you can use the folowing query:

<Query>
 <GroupBy Collapse="TRUE">
   <FieldRef Name="Title" />
 </GroupBy>
</Query>

But for counting of the items in the result set there is no direct OOB way. You can one thing when you get result set from the above query then from the item collection get it's count property and use it.

Otherwise, I can think other option:

get listitem in datatable and use linq to count on fields

 var query = from row in yourdatatable.AsEnumerable()
        group row by row.Field<Date>("Date") into ydate
        orderby ydate.Key
        select new
        {
            Name = ydate.Key,
            CountOforders = ydate.Count()
        };

// print result
foreach (var yourdate in query)
{
    Console.WriteLine("{0}\t{1}", yourdate.Name, yourdate.CountOforders);
}

Tags:

List