单个查询中的EF多个聚合

编程入门 行业动态 更新时间:2024-10-25 22:33:04
本文介绍了单个查询中的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多个聚合

本文发布于:2023-10-14 11:50:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1490976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   EF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!