I have a SharePoint list containing some items. I would like to apply grouping to the items using LINQ, preferably with .GroupBy method.

This is the list of fields that the list item is consisted of:

  • Title - Single line of text field
  • Date - Date and time field
  • Related - Lookup field

I need to have the items collection grouped by the Related lookup field. I am unable to figure out what to specify for GroupBy method parameters. The following code throws an error and prevents it being built.

var items = list.Items;
var groupedItems = items.Cast<SPListItem>().
    GroupBy(item => new SPFieldLookupValue(item["Related"].ToString()).LookupId);

Replacing LINQ with <GroupBy> element in list item query is not an option.

1 Answer 1


Hope you want to group by the LookupId, so you could do something like this:

var items = list.Items;
var groupedItems = items.OfType<SPListItem>().GroupBy(item => new SPFieldLookupValue(item["Related"]).LookupId);

The mistake should be you have converted the Object to string, then converting it to SPFieldLookupValue and trying to get LookupId from it. Instead do convert the Object directly to SPFieldLookupValue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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