0

I've done a GroupBy using Linq as follows in a controller:

model.aLstProducts = result[0].Results
    .Where(c => c.Id == ProductId)
    .ToList()
    .FirstOrDefault()
    .ListOfProducts
    .GroupBy(c => c.ProductCategory);

The property has been defined as follows:

public IEnumerable<IGrouping<string, ProductsViewModel>> aLstProducts { get; set; }

When I try to use it in the front-end using Razor, it throws the following exception:

@foreach (var item in Model.aLstProducts)
{ 
}

Error:

The type 'IGrouping<,>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

What could be the reason for the above or how can I overcome it?

N.B: I can work with generic List in the Razor view but for the GroupBy getting the exception.

Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32
user8512043
  • 1,043
  • 12
  • 20
  • Does this answer your question? [How to add a reference to assembly in C#?](https://stackoverflow.com/questions/21543093/how-to-add-a-reference-to-assembly-in-c) – Astrid E. Jan 15 '23 at 11:03

2 Answers2

0

At the beginning of razor page add

@model IEnumerable<IEnumerable<IGrouping<string, ProductsViewModel>>

then loop through the Model

  @foreach (var item in Model)
  {     
      // your code goes here    
  }
Morshed
  • 1
  • 3
0

use System.Linq at razor as well. ms doc

sina_Islam
  • 1,068
  • 12
  • 19