EF Core Linq使用相同列进行多个Count的方法

huangapple go评论76阅读模式
英文:

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);

概念验证

EF Core Linq使用相同列进行多个Count的方法

英文:

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

EF Core Linq使用相同列进行多个Count的方法

答案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;

huangapple
  • 本文由 发表于 2023年8月8日 23:12:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定