单个查询中的EF多个聚合 [英] EF multiple aggregate in single query

查看:133
本文介绍了单个查询中的EF多个聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据不同的条件来计算集合的数量:

I want to get count of a set based on different condition:

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

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

How its possible to call all three queries in one DB round trip to increase performance?

推荐答案

当然,只需将您的三个计数用POCO或匿名类型包装起来即可:

Sure, just wrap up your three counts in a POCO or anonymous type:

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

另外,如我所示,处理上下文。

Also, dispose your context, as I show.

这篇关于单个查询中的EF多个聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆