gpt4 book ai didi

entity-framework - 单个查询中的EF多个聚合

转载 作者:行者123 更新时间:2023-12-04 22:51:43 25 4
gpt4 key购买 nike

我想根据不同的条件获取一组计数:

 var invoices = new AccountingEntities().Transactions
var c1 = invoices.Count(i=>i.Type = 0);
var c2 = invoices.Count(i=>i.Type = 1);
var c3 = invoices.Count(i=>i.Type = 2);

如何在一次数据库往返中调用所有三个查询以提高性能?

最佳答案

当然,只需将您的三个计数包含在 POCO 或匿名类型中:

using (var invoices = new AccountingEntities())
{
var c = (from i in invoices.Transactions
select new
{
c1 = invoices.Count(i=>i.Type = 0),
c2 = invoices.Count(i=>i.Type = 1),
c3 = invoices.Count(i=>i.Type = 2)
}).Single();
}

另外,正如我展示的那样,处理您的上下文。

关于entity-framework - 单个查询中的EF多个聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4151580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com