How to handle IEnumerable with IGrouping in a view?

I am trying to send to an IEnumerable view and show every element in it, but I have a problem sending. It states that:

 {System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Linq.IGrouping<string,MvcApplication4.Models.USER>,<>f__AnonymousType8<string,int>>} 

and I don’t know how to handle this.

This is my opinion:

 @model IEnumerable<dynamic> 

I get dynamic because I get to this view from a couple of functions on how to send different types.

  <table border="1" style="text-align:center"> <tr> <td> </td> <td> user name </td> <td>age </td> <td>comments </td> </tr> @foreach (var t in (Model)) { <tr> <td>@(count++))</td> <td> @Console.WriteLine(t)</td> <td> </td> <td> </td> </tr> } </table> 

I get IEnumerable from this linq:

  var allU = (from m in comList join c in users on m.user equals c.user into cm1 from cm2 in cm1.DefaultIfEmpty() group cm2 by m.user into grouped select new { name = grouped.Key, count = grouped.Count() }).AsEnumerable(); 

So my question really is: how can I get its elements in a view?

+4
source share
1 answer

You cannot reference a type in your view because it is an anonymous type. Instead, determine the type to store the results of your query:

 public class Result { public string Name { get; set; } public int Count { get; set; } } 

Then change your project request to this type:

 var allU = (from m in comList join c in users on m.user equals c.user into cm1 from cm2 in cm1.DefaultIfEmpty() group cm2 by m.user into grouped select new Result { Name = grouped.Key, Count = grouped.Count() }).AsEnumerable(); 

Then you can bind to the IEnumerable<Result> in your view.

+3
source

All Articles