skip to Main Content

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.

2

Answers


  1. 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    
      }
    
    Login or Signup to reply.
  2. use System.Linq at razor as well.
    ms doc

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search