EFCore2.2中使用GroupBy的那些坑及解决方法

更新时间:2023-06-06 02:45:53 阅读: 评论:0

EFCore2.2中使⽤GroupBy的那些坑及解决⽅法
背景
  在后端使⽤EFCore进⾏数据库操作的时候,不可避免的要进⾏Group By操作,在进⾏Group By后有时候进⾏Sum的操作的时候EFCore是不能按照我们的预期⽣成正确的SQL的,⽽且这个问题EFCore的官⽅也没有给出⼀个好的解决⽅式,那么在使⽤EFCore2.2进⾏开发的时候这些问题⼜不可避免,那么我们该如何规避这个问题,并且使之⽣成我们期望的SQL语句呢,本篇我们我们就这个出现这个问题的现象以及解决⽅案来进⾏分析,来看看在现阶段到底该如何解决这个问题。
正常情况
  我们先来看⼀下能够正常⽣成SQL的EFCore的写法,这种情况下会⽣成符合预期的SQL语句并且没有什么问题,我们先来看看正常情况的EFCore的代码。
扬尘治理/// <summary>
/// 查询⽉度辅导任务清单列表
/// </summary>
/// <param name="input">查询输⼊</param>
/// <param name="pageRequest">分页请求</param>
/// <returns>带分页的⽉度任务清单</returns>
public async Task<Page<GetCoachTaskDetailOutput>> GetCoachTaskDetailExAsync(GetCoachTaskDetailInput input, PageRequest pageRequest) {
var queryResults = from v in from coachTask in _coachTaskRepository.GetAll()
.WhereIf(!string.IsNullOrWhiteSpace(input.Code), c => c.Code.Contains(input.Code))
.WhereIf(input.Year.HasValue, c => c.Year == input.Year)
.WhereIf(input.Quarter.HasValue, c => c.Quarter == input.Quarter)
.WhereIf(input.Month.HasValue, c => c.Month == input.Month)
.WhereIf(!string.IsNullOrWhiteSpace(input.MarketingResponsibleName),
c => c.MarketingResponsibleName.Contains(input.MarketingResponsibleName))
.WhereIf(input.MarketingDepartmentIds.Any(), c => input.MarketingDepartmentIds.Contains(c.MarketingDepartmentId))
join coachTaskDetail in _coachTaskDetailRepository.GetAll()
.WhereIf(!string.IsNullOrWhiteSpace(input.DealerCode), d => d.DealerCode.Contains(input.DealerCode))
.WhereIf(input.Status.Any(), d => input.Status.Contains(d.Status))
.WhereIf(input.ReviewStatus.HasValue, d => d.ReviewStatus == input.ReviewStatus)
.WhereIf(input.BeginActualCoachTime.HasValue, d => input.BeginActualCoachTime <= d.ActualCoachTime)
.WhereIf(input.EndActualCoachTime.HasValue, d => d.ActualCoachTime <= input.EndActualCoachTime)棉花胡同
on coachTask.Id equals coachTaskDetail.CoachTaskId
join coachTaskItemDetail in _coachTaskItemDetailRepository.GetAll()
.WhereIf(input.Type.HasValue, i => i.Type == input.Type)
on coachTaskDetail.Id equals coachTaskItemDetail.CoachTaskDetailId
lect new {
coachTaskDetail.Id,
coachTask.Code,
coachTask.Year,
coachTask.Quarter,
coachTask.Month,
coachTaskDetail.DealerCode,
coachTaskDetail.DealerName,
coachTaskDetail.Status,
素食菜谱大全素斋
coachTaskDetail.ReviewStatus,
coachTask.MarketingDepartmentName,
coachTask.MarketingResponsibleName,
coachTaskDetail.ActualerCoachName,
coachTaskDetail.ActualCoachTime,
coachTaskItemDetail.StandardScore,
coachTaskItemDetail.IsQualified
}
group v by new {
v.Id,
v.Code,
v.Year,
v.Quarter,
v.Month,
v.DealerCode,
v.DealerName,
v.Status,
v.ReviewStatus,
v.MarketingDepartmentName,
v.MarketingResponsibleName,
v.ActualerCoachName,
v.ActualCoachTime,
} into tempGroups
lect new GetCoachTaskDetailGroupByResultModel {
Id = tempGroups.Key.Id,
Code = tempGroups.Key.Code,
Year = tempGroups.Key.Year,
Quarter = tempGroups.Key.Quarter,
Month = tempGroups.Key.Month,
DealerCode = tempGroups.Key.DealerCode,
DealerName = tempGroups.Key.DealerName,
Status = tempGroups.Key.Status,
ReviewStatus = tempGroups.Key.ReviewStatus,
MarketingDepartmentName = tempGroups.Key.MarketingDepartmentName,
MarketingResponsibleName = tempGroups.Key.MarketingResponsibleName,
ActualerCoachName = tempGroups.Key.ActualerCoachName,
迅速造句ActualCoachTime = tempGroups.Key.ActualCoachTime,
//对应的⽉度辅导任务项⽬清单中标准分值
ScoreTotal = tempGroups.Sum(r => r.StandardScore ?? 0),
NoQualifiedQty = tempGroups.Sum(r => r.IsQualified == fal ? 1 : 0)警察手势
};
//获取分组后的结果
var totalCount = await queryResults.CountAsync();
var pagedResults = await queryResults.ProjectTo<GetCoachTaskDetailOutput>(_autoMapper.Config
urationProvider).PageAndOrderBy(pageRequest).ToListAsync();            pagedResults.ForEach(item => {
if (item.ScoreTotal < 0) {
item.ScoreTotal = 0;
}
});
return new Page<GetCoachTaskDetailOutput>(pageRequest, totalCount, pagedResults);
}
  上⾯的写法⾮常简单就是三张表进⾏join然后进⾏分组,只不过是最终的结果中ScoreTotal和NoQualifiedQty需要根据分组的结果进⾏求和,对于这种⽐较简单的分组结果EFCore是完全能够⽣成正确的SQL,我们来看看此时EFCore正确⽣成的SQL。洗碗的作文
SELECT TOP (20)
[coachTaskDetail].[ActualCoachTime],
[coachTaskDetail].[ActualerCoachName],
[coachTask].[Code],
[coachTaskDetail].[DealerCode],
COALESCE([coachTaskDetail].[Id], '00000000-0000-0000-0000-000000000000') AS [Id],对消
[coachTask].[MarketingDepartmentName],
[coachTask].[MarketingResponsibleName],
[coachTask].[Month],
COALESCE(SUM(CASE
WHEN [coachTaskItemDetail].[IsQualified] = 0
THEN 1
ELSE 0
END), 0)                                                    AS [NoQualifiedQty],
[coachTask].[Quarter],
COALESCE([coachTaskDetail].[ReviewStatus], 0)                            AS [ReviewStatus],
COALESCE(SUM(COALESCE([coachTaskItemDetail].[StandardScore], 0.0)), 0.0) AS [ScoreTotal],
[coachTaskDetail].[DealerName]                                          AS [ShortName],
COALESCE([coachTaskDetail].[Status], 0)                                  AS [Status],
COALESCE([coachTask].[Year], 0)                                          AS [Year]
FROM [CoachTask] AS [coachTask]
INNER JOIN [CoachTaskDetail] AS [coachTaskDetail] ON [coachTask].[Id] = [coachTaskDetail].[CoachTaskId]
怎么办物流公司
INNER JOIN [CoachTaskItemDetail] AS [coachTaskItemDetail]
ON [coachTaskDetail].[Id] = [coachTaskItemDetail].[CoachTaskDetailId]
GROUP BY [coachTaskDetail].[Id], [coachTask].[Code], [coachTask].[Year], [coachTask].[Quarter], [coachTask].[Month],
[coachTaskDetail].[DealerCode], [coachTaskDetail].[DealerName], [coachTaskDetail].[Status],
[coachTaskDetail].[ReviewStatus], [coachTask].[MarketingDepartmentName], [coachTask].[MarketingResponsibleName],
[coachTaskDetail].[ActualerCoachName], [coachTaskDetail].[ActualCoachTime]
ORDER BY [coachTask].[Code] DESC
  未完,待续......

本文发布于:2023-06-06 02:45:53,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/879296.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:问题   结果   时候   情况   辅导   语句
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图