传入字典的模型项的类型为'System.Linq.Enumerable+

本文关键字:Linq System Enumerable+ TakeIterator 字典 模型 类型 | 更新日期: 2023-09-27 18:16:32

所以我是新的Asp和MVC,我使用Lambda表达式为了排序列表,并采取前五名,但我不断得到一个模型项目传入字典的类型是'System.Linq。可枚举的+d__3a错误请帮助我的代码下面

public ActionResult Queue()
    {
        var context = new ApplicationDbContext();
        var form = context.Forms..OrderBy(c => c.Date).ToList().Take(5);
        return View(form);
    }
 [Required]
    [Display(Name = "Date Visited")]
    [DataType(DataType.Date)]
    public DateTime Date { get; set; }

 <tbody>
    @foreach (var form in Model)
    {
        <tr>
            <td>
                @Html.ActionLink("view", "AdminApproval", new { id = form.FormId })
            </td>
            <td>
                @form.Submitter
            </td>
            <td>
                HomeClub
            </td>
            <td>
                @form.ClubVisited
            </td>
            <td>
                @form.Date
            </td>
        </tr>
    }
</tbody>

传入字典的模型项的类型为'System.Linq.Enumerable+<TakeIterator>

默认情况下,LINQ使用延迟执行,即代码在遍历项时实际执行。

Take - Use the Take<TSource> operator to return a given number of elements in a sequence and then skip over the remainder.
http://msdn.microsoft.com/en-us/library/bb386988%28v=vs.110%29.aspx

使用ToList - The ToList<TSource>(IEnumerable<TSource>) method forces immediate query evaluation and returns a List<T> that contains the query results. You can append this method to your query in order to obtain a cached copy of the query results.

http://msdn.microsoft.com/en-us/library/vstudio/bb342261%28v=vs.100%29.aspx

我们需要将实际的集合返回给View。所以先调用Take(5)再调用ToList()

return View(context.Forms.OrderBy(c => c.Date).Take(5).ToList())
http://www.dotnetcurry.com/showarticle.aspx?ID=750

变化:

var form = context.Forms.OrderBy(c => c.Date).ToList().Take(5);

:

var form = context.Forms.OrderBy(c => c.Date).Take(5).ToList();