Return 使用 linq 按两个属性分组的列表 ASP.NET MVC

Return List grouped by two properties using linq ASP.NET MVC

所以我有这个 Table 和 class Report:

基于 ProductIdReportType 我想创建一个 returns 和 List<ReportInnerResponse> 的 LINQ 表达式,其中 ReportInnerResponse 包含以下内容:

    public class ReportInnerResponse
    {
        public int Id { get; set; }
        public ReportType ReportType { get; set; }
        public int ReportCount { get; set; }
    }

所以我的最终结果是这样的:

这是我当前的代码,但它不能正常工作:

        public ActionResult GetReportsById(int id)
        {
            var reports = DbContext.Reports.GroupBy(x => new {x.ProductId, x.ReportType}).Select(g => g.FirstOrDefault()).Where(x => x.ProductId == id).Select(r =>
            new Models.Response.ReportInnerResponse
            {
                Id = r.Id,
                ReportType = r.ReportType,
                ReportCount = DbContext.Reports.Where(x => x.ReportType == r.ReportType && x.ProductId == id).Count()
            }).ToList();
            return PartialView("_ProductReportDetails", reports);
        }

主视图:

@model IEnumerable<Finder.Models.Response.AdminReportResponse>

@{
    ViewBag.Title = "Report Panel | Finder";
}

<h2>Report Panel</h2>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Image)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            Seller
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th>
            Report Info
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                <img src="@item.Image" height="100px" width="100px" style="object-fit: contain" />
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FullName)
            </td>
            <td>
                @{ 
                    var price = item.Price.ToString("C");
                }
                @price
            </td>
            <td>
                @if (item.ReportCount >= 1)
                {
                    <span>@item.ReportCount</span>
                    @Html.Action("GetReportsById", new { id = item.Id })
                }
            </td>
        </tr>
    }

</table>

局部视图:


@model List<Finder.Models.Response.ReportInnerResponse>

<!-- Button trigger modal -->

<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModalCenter">
    <i class="fa-solid fa-flag" style="margin-right:10px;"></i> See Reports
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document" style="width: 100%">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Report Menu</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                @foreach (var item in Model)
                {
                    <p><span>@item.ReportCount</span>@item.ReportType</p>
                }
            </div>
        </div>
    </div>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

如有任何帮助,我们将不胜感激。

Id 属性 on Models.Response.ReportInnerResponse 没有多大意义,TBH,因为它代表分组结果(或者它应该被命名为 ProductId)。删除它并尝试以这种方式重写您的查询:

var reports = DbContext.Reports
    .Where(x => x.ProductId == id) // filter out not needed data before grouping
    .GroupBy(x => x.ReportType)
    .Select(g => new Models.Response.ReportInnerResponse
        {
            ReportType = g.Key,
            ReportCount = g.Count()
        })
    .ToList();