英文:
EF Core Linq with Multiple Count Using the Same Column
问题
我正在根据相同列的条件计算记录的总数,基本上这就是我现在拥有的代码:
var checkTotal = await _dbRO.AllOpenExceptions
.Where(x => x.ClientId == _user.ClientId)
.CountAsync(x => x.ExceptionType == "Check");
var achTotal = await _dbRO.AllOpenExceptions
.Where(x => x.ClientId == _user.ClientId)
.CountAsync(x => x.ExceptionType == "ACH");
如你所见,我只需要计算 ExceptionType 列为 "Check" 和 "ACH" 的记录数。有没有办法将这两个单独的查询合并为一个查询?
我尝试过使用 group by 和 select,但无法找到确切的语法。感谢任何帮助!
英文:
I am counting the total number of records based on a condition from the same column, basically, this is what I have now:
var checkTotal = await _dbRO.AllOpenExceptions
.Where(x => x.ClientId == _user.ClientId)
.CountAsync(x => x.ExceptionType == "Check");
var achTotal = await _dbRO.AllOpenExceptions
.Where(x => x.ClientId == _user.ClientId)
.CountAsync(x => x.ExceptionType == "ACH");
As you can see, I just need to count the number of records for ExceptionType column is "Check" vs. "ACH". Is there any way to combine these two separate queries into a single query?
I've tried group by with select but couldn't figure out the exact syntax. Thanks for any help!
答案1
得分: 3
我们可以使用clientId和exceptionType对数据进行分组和筛选,然后将其转换为字典:
var totals = await _dbRO.AllOpenExceptions
.Where(x => x.ClientId == clientId && (x.ExceptionType == "ACH" || x.ExceptionType == "Check"))
.GroupBy(x => new { x.ClientId, x.ExceptionType })
.ToDictionaryAsync(
key => key.Key.ExceptionType,
value => value.Count(),
cancellationToken);
如果你没有取消令牌,可以将其删除。现在你可以通过键"ACH"或"Check"来访问你的总数:
var achTotal = totals["ACH"];
var checkTotal = totals["Check"];
进一步避免一些异常情况,你甚至可以使用TryGetValue方法:
totals.TryGetValue("ACH", out var achTotal);
totals.TryGetValue("Check", out var checkTotal);
概念验证
英文:
We can group and filter data using the clientId and exceptionType and then convert it to a dictionary:
var totals = await _dbRO.AllOpenExceptions
.Where(x => x.ClientId == clientId && (x.ExceptionType == "ACH" || x.ExceptionType == "Check"))
.GroupBy(x => new { x.ClientId, x.ExceptionType })
.ToDictionaryAsync(
key => key.Key.ExceptionType,
value => value.Count(),
cancellationToken);
If you don't have a cancellation token, just get rid of it. And now you can access your total by key ACH or Check.
var achTotal = totals["ACH"];
var checkTotal = totals["Check"];
Going a bit further and avoiding some exceptions, you could even use the TryGetValue method:
totals.TryGetValue("ACH", out var achTotal);
totals.TryGetValue("Check", out var checkTotal);
Proof of concept
答案2
得分: 1
你可以使用GroupBy
来实现这个功能:
var totals = await _dbRO.AllOpenExceptions
.Where(x => x.ClientId == _user.ClientId)
.GroupBy(x => 1)
.Select(g => new
{
CheckCount = g.Sum(x => x.ExceptionType == "Check" ? 1 : 0),
AchCount = g.Sum(x => x.ExceptionType == "ACH" ? 1 : 0)
})
.FirstOrDefaultAsync();
var checkTotal = totals?.CheckCount ?? 0;
var achTotal = totals?.AchCount ?? 0;
英文:
You can do that with GroupBy
:
var totals = await _dbRO.AllOpenExceptions
.Where(x => x.ClientId == _user.ClientId)
.GroupBy(x => 1)
.Select(g => new
{
CheckCount = g.Sum(x => x.ExceptionType == "Check" ? 1 : 0),
AchCount = g.Sum(x => x.ExceptionType == "ACH" ? 1 : 0)
)
.FirstOrDefaultAsync();
var checkTotal = totals?.CheckCount ?? 0;
var achTotal = totals?.AchCount ?? 0;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论