C# 使用视图模型类进行求和和和视图的LINQ查询
我的课程和样本数据C# 使用视图模型类进行求和和和视图的LINQ查询,c#,.net,asp.net-mvc,linq,asp.net-mvc-4,C#,.net,Asp.net Mvc,Linq,Asp.net Mvc 4,我的课程和样本数据 public class BudgetModels { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int BudgetId { get; set; } public int BudgetType { get; set; } public string BudgetName { get; set; }
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,00,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,00,000 1 1
2 1 3,00,000 1 2
3 2 6,00,000 2 2
4 2 2,00,000 2 1
5 2 1,00,000 1 1
我必须做一份报告,每季度从每个预算中拿出多少钱给学校
报告格式::
BudgetName BudgetType BudgetAmount MappedType TotalMappedAmount RemainingAmount
B1 101 5,00,000 1 4,00,000 1,00,000
B1 101 5,00,000 2 1,00,000 0
B2 201 10,00,000 1 0 10,00,000
B2 201 10,00,000 2 8,00,000 2,00,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
,并因此得到一组映射的
项。这就是为什么您的模型budgetquartryreportviewmodel
不适合这里的原因
但是,如果您可以这样定义一个模型:
类BudgetQuaterryReportGroupModel
{
公共int映射类型{get;set;}
公共列表报告{get;set;}
公共十进制TotalMappedAmount{get;set;}
公共十进制RemainingAmount{get;set;}
}
然后您将能够执行以下操作:
var result=(从db.Mappeds中的n开始)
将k加入n.BudgetId上的db.Budgets等于k.BudgetId
将n按n分组。将类型映射为g
选择新的BudgetQuartryReportGroupModel
{
MappedType=g.键,
Reports=g.Select(i=>newbudgetquartryreportviewmodel
{
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),
remaingamount=//这里有一些逻辑
});
注意事项:
- 对于上述分组,您不需要按
new
运算符分组,除非您希望按多个属性分组,例如:
。。。
按新的{n.MappedType,n.Budget.BudgetName}将n分组为g
...
- 对于希望将分组和联接作为单个操作组合在一起的情况,请考虑使用
- 甚至在编写查询之前,一定要写下您想要的结果,并一步一步地分析您的查询,试图融入您的设计
希望这会有帮助)您使用的LINQ是什么:LINQ到SQL/EF 6.x/EF Core 2.0/EF Core 2.1/EF Core 3.x?我正在使用EF 6.x谢谢!