C# 请将下列表用lambda按照ServiceDate,ServiceBranchCode,ClueID,ProjectType,ServiceProjectTemplateID分组并计算ServiceCount,SaleAmount总和 CREATE TABLE dbo.ReportConsultantAchievementDetail( ID int IDENTITY(1,1) NOT NULL, CreatedBy varchar(16) NOT NULL, CreatedDate datetime NOT NULL, ModifiedBy varchar(16) NOT NULL, ModifiedDate datetime NOT NULL, HasDeleted bit NOT NULL, OrderCode varchar(20) NOT NULL, BranchCode varchar(16) NOT NULL, ServiceDate datetime NOT NULL, ClueID int NOT NULL, ServiceProjectTemplateID int NOT NULL, CustName nvarchar(32) NOT NULL, ItemCode varchar(16) NOT NULL, ItemName nvarchar(32) NOT NULL, ItemClass varchar(2) NOT NULL, IsCooperation int NOT NULL, CooperationName nvarchar(32) NOT NULL, ProjectType int NOT NULL, Source int NOT NULL, SourceID int NOT NULL, ServiceCount decimal(18,2) NOT NULL, OriginalAmount decimal(18,2) NOT NULL, Discount decimal(18,2) NOT NULL, SaleAmount decimal(18,2) NOT NULL, ConsultantUserCode varchar(16) NOT NULL, ServiceBranchName nvarchar(16) NOT NULL, ServiceBranchCode varchar(16) NOT NULL, Remark nvarchar(300) NULL, ExamineStatus int NOT NULL, ExamineTime datetime NULL, ExamineBy varchar(16) NULL )
时间: 2024-03-11 08:49:00 浏览: 50
C#实现数值计算方法
5星 · 资源好评率100%
可以使用以下代码实现:
```
var result = dbContext.ReportConsultantAchievementDetail
.GroupBy(d => new
{
d.ServiceDate,
d.ServiceBranchCode,
d.ClueID,
d.ProjectType,
d.ServiceProjectTemplateID
})
.Select(g => new
{
ServiceDate = g.Key.ServiceDate,
ServiceBranchCode = g.Key.ServiceBranchCode,
ClueID = g.Key.ClueID,
ProjectType = g.Key.ProjectType,
ServiceProjectTemplateID = g.Key.ServiceProjectTemplateID,
ServiceCount = g.Sum(x => x.ServiceCount),
SaleAmount = g.Sum(x => x.SaleAmount)
});
```
需要注意的是,在使用 Lambda 表达式进行分组和聚合操作时,需要使用 `GroupBy` 方法将数据按照特定的字段分组,然后使用聚合函数(如 `Sum`)对分组后的数据进行计算。最后,将计算结果映射为一个匿名类型并返回给调用方。
阅读全文