Stack Exchange
Group by in c# for Document Directory having Column contain same value
I have Document library which has Document Content type and a Column named Engagement , whose values are populated from term store.
Engagement Column value:

AAA 
BBB 
CCC 
DDD

Now my requirement is to group all records by Engagement Column value
My  ref code:
//Column Field Name
  private string _EI_Field_Name = "Engagement_x0020_and_x0020_Inclusion_x0020_Document";
    List<SPListItem> listItems = list.Items.Cast<SPListItem>().ToList();
    if (listItems.Count > 0)
    {
        var groupedItems = listItems.GroupBy(a => a[_EI_Field_Name]).ToDictionary(g=>g.Key);                                  
        foreach (var groupItem in groupedItems)
        {
            foreach (var groupValue in groupItem)
            {
            }
        }
    }


Try
var groupedItems =  listItems
      .OfType()
      .GroupBy(item => ((TaxonomyFieldValue)item[_EI_Field_Name]).TermGuid)
      .ToList();
