我正在尝试向 View 发送 IEnumerable
并显示其中的每个元素,但我发送它时遇到问题。它说:
{System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Linq.IGrouping<string,MvcApplication4.Models.USER>,<>f__AnonymousType8<string,int>>}
我不知道如何处理它。
这是我的观点:
@model IEnumerable<dynamic>
我得到
dynamic
因为我从几个函数中看到了这个 View 如何发送不同的类型。 <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>
我得到
IEnumerable
从这个 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();
所以我的问题真的是:我怎样才能在 View 中获取它的元素?
最佳答案
您不能在 View 中引用该类型,因为它是匿名类型。相反,定义一个类型来存储查询结果:
public class Result
{
public string Name { get; set; }
public int Count { get; set; }
}
然后将您的查询更改为该类型:
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();
然后你就可以绑定(bind)到类型
IEnumerable<Result>
在你看来。
关于linq - 如何在 View 中使用 IGrouping 处理 IEnumerable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14293990/