Using LINQ to loop through data and display in a t

2019-08-20 10:08发布

问题:

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

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?

Thanks,

回答1:

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


回答2:

It seems to me that your two queries can be combined and simplified a bit: just take this part:

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

Which will return a collection of IGrouping<TKey, TElement>. You can iterate over the collection of groupings where the key will be the user and the value will be the collection of items associated with the user. For example:

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