LINQ使用视图模型类查询总和和视图

问题描述

我的课以及示例数据,

public class BudgetModels
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int BudgetId { get; set; }
        public int BudgetType { get; set; }
        public string BudgetName { get; set; }
        public decimal BudgetAmount { get; set; }
    }
BudgetId    BudgetType      BudgetName          BudgetAmount
1              101              B1              5,00,000
2              201              B2              10,000   
    
public class SchoolModels 
    {
        [Key]        
        public int SchoolId { get; set; }       
        public string SchoolName { get; set; }
    }
SchoolId        SchoolName
1                 NJ.EBS
2                 LA.EBS

    
public class MappedModels
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int MappedId { get; set; }
        public int MappedType { get; set; } // quarterly basis first quarter,second quarter,third quarter
        public decimal Mappedamount { get; set; }
        [ForeignKey("BudgetId")]
        public BudgetModels Budget { get; set; } 
        public int BudgetId { get; set; }
        
        [ForeignKey("SchoolId")]
        public SchoolModels School { get; set; }
        public int SchoolId { get; set; }
    }

MappedModels的数据

MappedId    MappedType      Mappedamount    BudgetId    SchoolId
1               1               1,000        1           1
2               1               3,000        1           2
3               2               6,000        2           2
4               2               2,000        2           1
5               2               1,000        1           1

我必须做一个报告,以便每季度将每笔预算中的多少钱分配给学校。

报告格式::

BudgetName  BudgetType  BudgetAmount    MappedType      TotalMappedamount       RemainingAmount
B1              101         5,000        1                4,000               1,000
B1              101         5,000        2                1,000                   0
B2              201         10,000       1                   0                   10,000               
B2              201         10,000       2                8,000               2,000

我已经为此报告创建了视图模型类:

public class BudgetQuarterlyReportviewmodel
    {
        public int BudgetId { get; set; }
        public string BudgetName { get; set; }       
        public int BudgetType { get; set; }
        public decimal BudgetAmount { get; set; }
        public int MappedType { get; set; }
        public decimal TotalMappedamount { get; set; }
        public decimal RemainingAmount { get; set; }
    }

我尝试了什么::

// BudgetModels Controller
public ActionResult Index()
        {

            var result = (from n in db.Mappeds
                          join k in db.Budgets on n.BudgetId equals k.BudgetId
                          //group n by new { n.MappedType} into g
                          select new BudgetQuarterlyReportviewmodel()
                          {
                              BudgetId = k.BudgetId,BudgetType = k.BudgetType,BudgetName = k.BudgetName,BudgetAmount = k.BudgetAmount,MappedType = n.MappedType,TotalMappedamount =,}

                         ).ToList();
            return View(result);
        }

查询遇到问题,例如我无法对Mappedamount求和,并且在取消注释group语句时,在select块中出错。我也不确定我的LINQ语句是否正确。

解决方法

好吧,让我们首先了解按MappedType分组时发生的情况。
分组后,您将获得一个MappedType密钥,并因此获得了Mapped项的集合。这就是为什么您的模型BudgetQuarterlyReportViewModel不适合这里的原因。

但是,相反,如果您可以定义这样的模型:

class BudgetQuarterlyReportGroupModel 
{
    public int MappedType { get; set; }
    public List<BudgetQuarterlyReportViewModel> Reports {get; set; }
    public decimal TotalMappedAmount { get; set; }
    public decimal RemainingAmount { get; set; }
}

然后您将能够执行此操作:

var result = (from n in db.Mappeds
    join k in db.Budgets on n.BudgetId equals k.BudgetId
    group n by n.MappedType into g
    select new BudgetQuarterlyReportGroupModel
    {
        MappedType = g.Key,Reports = g.Select(i => new BudgetQuarterlyReportViewModel
        {
            MappedType = i.MappedType,BudgetAmount = i.Budget.BudgetAmount,BudgetName = i.Budget.BudgetName,BudgetType = i.Budget.BudgetType,BudgetId = i.BudgetId
        }).ToList(),TotalMappedAmount = g.Sum(m => m.MappedAmount),RemainingAmount = // some logic here 
    });

注释:

  • 对于上述分组,除非您要按多个属性进行分组,否则无需按new运算符进行分组,例如:
...
group n by new {n.MappedType,n.Budget.BudgetName} into g
...
  • 如果要将分组和联接合并为一个操作,请考虑使用GroupJoin
  • 在编写查询之前,请务必写下您想要的结果并逐步进行分析,以尝试进入您的设计。

希望这会有所帮助))