[Vuejs]-Linq Groupby Not Grouping in c#

2👍

You need to assign result of GroupBy() to variable:

returnList = returnList.GroupBy(rec => rec.PropertyAddress).ToList();

2👍

Make sure to actually use the new IEnumerable that the .GroupBy() Method returned.

If you want to return a List you need to use a workaround:

  1. Get the IEnumerable<IGrouping<int, ReturnRecord>> from the .GroupBy()
  2. Use .SelectMany() to select all elements and save them into an IEnumerable
  3. Now you can convert your IEnumerable into a List with .List()

Example:

// Longer Alternative
IEnumerable<IGrouping<int, ReturnRecord>> groups = resultList
    .GroupBy((rec => rec.PropertyAddress);
IEnumerable<ReturnRecord> result = groups.SelectMany(group => group);
List<ReturnRecord> listResult = result.ToList();
return Ok(listResult);

// Shorter Alternative
IEnumerable<IGrouping<int, ReturnRecord>> groups = resultList
    .GroupBy((rec => rec.PropertyAddress);
IEnumerable<ReturnRecord> result = groups.SelectMany(group => group);
return Ok(result.ToList());

Leave a comment