【问题标题】:Using LINQ to loop through data and display in a table使用 LINQ 循环遍历数据并在表格中显示
【发布时间】:2019-05-21 07:47:15
【问题描述】:

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

我的 Razor 页面类似于以下内容(为简洁起见已缩减)。

@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 创建列表并填充表格,如下所示:

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();

寻找如下的最终结果:

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

谢谢,

【问题讨论】:

  • 您遇到了在前端进行表格格式化或在数据表中获取数据的问题。

标签: c# asp.net linq asp.net-core razor


【解决方案1】:

您绑定模型错误。不要在代码中创建DataList,而是在 Razor 页面的内部 foreach 循环之前创建它。(或在它之前发送空填充)

@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>
}

【讨论】:

  • 谢谢@Mayk。我收到Operator '==' cannot be applied to operands of type 'string' and 'LoadTable'。以及foreach statement cannot operate on variables of type ?。任何想法?另外,我猜这种方式会使我的第二个列表变得不必要?
  • 谢谢,这似乎没问题,只有它;每人仅显示 1 行数据。我现在正在看,但如果您有任何想法,我将不胜感激。
  • 这是您填写TableList 的方式。检查那个。特别是 groupby 和 select group.First() 部分。
  • 快到了。它给了我正确的数据,除了它循环了太多次。它循环查找每条记录的匹配数,并为每条记录提供一个表格,而不是每个用户一次。感谢您的帮助。
  • 在此答案的上下文中,您是否可以为我的其他问题提供任何输入? stackoverflow.com/questions/56237256/…
【解决方案2】:

在我看来,您的两个查询可以合并并简化一点:只需这部分:

_context
    .LoadTable
    .Where(o => o.Approver == GetADDetails.displayName || o.Approver == GetADDetails.userName)
    .GroupBy(user => user.SubmittedBy);

这将返回IGrouping&lt;TKey, TElement&gt; 的集合。您可以遍历分组集合,其中键是用户,值是与用户关联的项目的集合。例如:

foreach (var userWithItems in Model.TableList)
{
    var user = userWithItems.Key;

    // TODO: Render the table per user here

    foreach (var item in userWithItems)
    {
        // TODO: Render the items for the user here
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-29
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    相关资源
    最近更新 更多