Home > OS >  IGrouping Not Found in Razor View
IGrouping Not Found in Razor View

Time:01-22

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.

CodePudding user response:

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    
  }

CodePudding user response:

use System.Linq at razor as well. ms doc

  • Related