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语句是否正确。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)