【问题标题】:ASP.Netcore MVC - Razor Displaying expense totals for categories by Month/Year rows and total expenses per month using efcoreASP.Net Core MVC - Razor 使用 ef core 按月/年行显示类别的费用总额和每月的总费用
【发布时间】:2020-03-31 07:00:21
【问题描述】:

I am trying to create a table like the one below, with orders grouped by month and the perspective catergories that the orders fall under are totaled accordingly

我还能够按月/年排序并获得总计或按类别排序并获得该类别的总金额 - 但无法弄清楚如何将它们组合在一起。

这将创建允许月份组的操作

    public async Task<IActionResult> ExpensesPaymentList()
    {

        var model = _context.Requests.Include(r => r.Product)
            .ThenInclude(r => r.ProductSubcategory)
            .ThenInclude(r => r.ParentCategory)
            .GroupBy(r => new
            {
                Month = r.ParentRequest.OrderDate.Month,
                Year = r.ParentRequest.OrderDate.Year,
            })

            .Select(g => new MonthlyTotalsViewModel
            {
                Month = g.Key.Month,
                Year = g.Key.Year,
                GrandTotal = g.Sum(r => r.Cost)
            })
            .OrderByDescending(a => a.Year)
            .ThenByDescending(a => a.Month)
            .ToList();

        return View(await model.ToListAsync());
    }

这会创建允许类别组的操作

    public ActionResult ExpensesPaymentList()
    {
        var model = _context.Requests.Include(r => r.Product)
            .ThenInclude(r => r.ProductSubcategory)
            .ThenInclude(r => r.ParentCategory)
            .AsEnumerable()
            .GroupBy(r => new
            {
                ParentCategory = r.Product.ProductSubcategory.ParentCategory,
            })
            .Select(g => new ExpensesPaymentListViewModel
            {
                ParentCategory = g.Key.ParentCategory,
                Total = g.Sum(r => r.Cost)
            })
            .ToList();

        return View(model);
    }

我的视图模型:

public class ExpensesPaymentListViewModel
{
    public ParentCategory ParentCategory { get; set; }
    public double Total { get; set; }
}
public class MonthlyTotalsViewModel
{
    public int Month { get; set; }
    public int Year { get; set; }
    public double GrandTotal { get; set; }
}

我愿意接受任何建议或尝试全新的东西 - 我已经坚持了好几天了。 谢谢!

编辑: 我还尝试在选择中使用 where 并对各种类别进行硬编码并将它们绑定到 viewModel 但是 Linq 说它不是有效的查询。见下文

    public async Task<IActionResult> ExpensesPaymentList()
    {

        var model = _context.Requests.Include(r => r.Product)
            .ThenInclude(r => r.ProductSubcategory)
            .ThenInclude(r => r.ParentCategory)
            .GroupBy(r => new
            {
                Month = r.ParentRequest.OrderDate.Month,
                Year = r.ParentRequest.OrderDate.Year,
            })

            .Select(g => new MonthlyTotalsViewModel
            {
                Month = g.Key.Month,
                Year = g.Key.Year,
                PlasticsTotal = g.Where(g => g.Product.ProductSubcategory.ParentCategory.ParentCategoryID == 1).Sum(r => r.Cost),
                ReagentsTotal = g.Where(g => g.Product.ProductSubcategory.ParentCategory.ParentCategoryID == 2).Sum(r => r.Cost), 
                  //and so on for the rest of my parentcategories
                GrandTotal = g.Sum(r => r.Cost)
            })
            .OrderByDescending(a => a.Year)
            .ThenByDescending(a => a.Month)
            .ToList();

        return View(await model.ToListAsync());
    }

【问题讨论】:

    标签: c# linq model-view-controller


    【解决方案1】:

    这不是同一种形式的逻辑,但应该可以解决问题。输出正是它所需要的

    型号:

    public class MonthlyTotalsModel
        {
    
            public int Month { get; set; }
            public int Year { get; set; }
            public double GrandTotal { get; set; }
            public double PlasticsTotal { get; set; }
            public double ReagentsTotal { get; set; }
            public double ProprietyTotal { get; set; }
            public double ReusableTotal { get; set; }
    
        }
    

    视图模型

        public class ExpensesListViewModel
        {
            public List<MonthlyTotalsViewModel> monthlyTotals { get; set; }
        } 
    

    控制器

    public async Task<IActionResult> ExpensesList()
        {
            List<MonthlyTotalsViewModel> monthlyTotals = new List<MonthlyTotalsViewModel>();
    
            var requestsByMonthAndYear = _context.Requests.Select(r => new { Year = r.ParentRequest.OrderDate.Year, Month = r.ParentRequest.OrderDate.Month }).Distinct();
            foreach (var req in requestsByMonthAndYear)
            {
                var requestsinMonthAndYear = _context.Requests.Include(r => r.ParentRequest).Include(r => r.Product).ThenInclude(p => p.ProductSubcategory).ThenInclude(p => p.ParentCategory).Where(r => r.ParentRequest.OrderDate.Year == req.Year).Where(r => r.ParentRequest.OrderDate.Month == req.Month).ToList();
                MonthlyTotalsViewModel monthlyTotalsViewModel = new MonthlyTotalsViewModel
                {
                    Month = req.Month,
                    Year = req.Year,
                    GrandTotal = requestsinMonthAndYear.Sum(r => r.Cost),
                    PlasticsTotal = requestsinMonthAndYear.Where(r => r.Product.ProductSubcategory.ParentCategoryID == 1).AsEnumerable().Sum(m => m.Cost),
                    ReagentsTotal = requestsinMonthAndYear.Where(r => r.Product.ProductSubcategory.ParentCategoryID == 2).AsEnumerable().Sum(m => m.Cost),
                    ProprietyTotal = requestsinMonthAndYear.Where(r => r.Product.ProductSubcategory.ParentCategoryID == 3).AsEnumerable().Sum(m => m.Cost),
                    ReusableTotal = requestsinMonthAndYear.Where(r => r.Product.ProductSubcategory.ParentCategoryID == 4).AsEnumerable().Sum(m => m.Cost)
                }
                monthlyTotals.Add(monthlyTotalsViewModel);
            }
    
            ExpensesListViewModel expensesListViewModel = new ExpensesListViewModel
            {
                monthlyTotals = monthlyTotals
            };
    
            return View(expensesListViewModel);
        }
    

    查看

    @model PrototypeWithAuth.ViewModels.ExpensesListViewModel
    @{
        ViewData["Title"] = "ExpensesList";
        Layout = "~/Views/Shared/PaymentView.cshtml";
    }
    
    <h1>ExpensesList</h1>
    
    <table class="table table-bordered">
    <tr>
        <th>
            Month - Year
        </th>
        <th>
            Plastics
        </th>
        <th>
            Reagents
        </th>
        <th>
            Proprietry
        </th>
        <th>
            Reusable
        </th>
    </tr>
    @foreach(var item in Model.monthlyTotals)
    {
        <tr>
            <td>
                @item.Month - @item.Year
            </td>
            <td>
                @item.PlasticsTotal
            </td>
            <td>
                @item.ReagentsTotal
            </td>
            <td>
                @item.ProprietyTotal
            </td>
            <td>
                @item.ReusableTotal
            </td>
        </tr>
    }
    </table>
    

    【讨论】:

      猜你喜欢
      • 2021-05-05
      • 2016-09-02
      • 1970-01-01
      • 2016-07-19
      • 2020-01-08
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多