使用LINQ遍历数据并在表中显示 [英] Using LINQ to loop through data and display in a table

查看:81
本文介绍了使用LINQ遍历数据并在表中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Razor页面在ASP.NET Core中编写一个应用程序.我正在尝试显示一组表中的数据集,每个人一个表(SubmittedBy),然后显示该人提交的表数据.

I'm writing an application in ASP.NET Core with Razor pages. I'm trying to display a data set in a set of tables with one table per person (SubmittedBy) and then table data that was submitted by that individual.

我的剃须刀页面"看起来类似于以下内容(为简洁起见而减少).

My Razor Page looks similar to the following (reduced for brevity).

@foreach (var user in Model.TableList)
{
    <table class="table">
        <thead>
            <tr>
                <th>@Html.DisplayNameFor(model => model.LoadTable.SubmittedBy): @Html.DisplayFor(modelItem => user.SubmittedBy)</th>
            </tr>
            <tr>
                <th>@Html.DisplayNameFor(model => model.LoadTable.UID)</th>
        ...
        ...
        ...
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.DataList)
            {
                <tr>
                    <th scope="row" class="text-center align-middle">
                        @Html.DisplayFor(modelItem => item.UID)
                    </th>
                    <td class="text-center align-middle">
                        @Html.DisplayFor(modelItem => item.Age)
                    </td>
            ...
            ...
            ...
        </tr>
        }
    </tbody>
    </table>
}

我正在使用LINQ创建列表并按如下方式填充表:

I'm using LINQ to create lists and populate the table as follows:

public IEnumerable<LoadTable> TableList;
public IEnumerable<LoadTable> DataList;

TableList = _context.LoadTable.Where(o => o.Approver == GetADDetails.displayName || o.Approver == GetADDetails.userName).GroupBy(user => user.SubmittedBy).Select(group => group.First()).ToList();
DataList = TableList.Where(p => TableList.Any(p2 => p2.SubmittedBy == p.SubmittedBy)).ToList();

寻找最终结果,如下所示:

Looking for an end result like the following:

我无法完全正确显示数据,并且缺少某些内容.谁能指出我正确的方向?

I can't quite get the data the display correctly and there's something missing. Can anyone point me in the right direction?

谢谢

推荐答案

您错误地绑定了模型.与其在代码中创建DataList而不是在Razor页面中的内部foreach循环之前创建它(或发送它).将其填满)

You are binding the model wrong.Instead of creatingDataList in the code just create it before the inner foreach loop in the Razor Page.(or send it empty fill before it)

@foreach (var user in Model.TableList)
{
    <table class="table">
        <thead>
            <tr>
                <th>@Html.DisplayNameFor(model => model.LoadTable.SubmittedBy): @Html.DisplayFor(modelItem => user.SubmittedBy)</th>
            </tr>
            <tr>
                <th>@Html.DisplayNameFor(model => model.LoadTable.UID)</th>
        ...
        ...
        ...
            </tr>
        </thead>
        <tbody>
            @(var DataList=Model.TableList.Where(p=>p.SubmittedBy==user.SubmittedBy).ToList();)
            @foreach (var item in DataList)
            {
                <tr>
                    <th scope="row" class="text-center align-middle">
                        @Html.DisplayFor(modelItem => item.UID)
                    </th>
                    <td class="text-center align-middle">
                        @Html.DisplayFor(modelItem => item.Age)
                    </td>
            ...
            ...
            ...
        </tr>
        }
    </tbody>
    </table>
}

这篇关于使用LINQ遍历数据并在表中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆