简体   繁体   English

LINQ查询具有分组和多个计数

[英]LINQ query with group by and multiple counts

I'm trying to write a LINQ query using linq-to-entities in EF 6 to retrieve a list of water basins and their associated counts. 我正在尝试使用EF 6中的linq-to-entities编写LINQ查询,以检索水盆及其相关计数的列表。 The issue is that he child tables are in opposing directions. 问题在于他的子桌子方向相反。

I wish to do this without using a sub-select within the select clause for performance reasons. 出于性能原因,我希望不使用select子句中的子选择来执行此操作。 In SQL I would normally created Derived Views but don't know how to accomplish this via Linq. 在SQL中,我通常会创建派生视图,但不知道如何通过Linq完成此操作。

A Basin has multiple Stations (Wells) associated to it as children. 一个盆地有多个与之关联的小站(井)。 As parents it can be utilized by multiple Organizations who manage the Basins. 作为父母,管理流域的多个组织可以利用它。 I need a count of Stations and a count of Organizations per Basin. 我需要一个站点数和每个盆地的组织数。

I've tried group by however it appears to only support aggregating in one direction. 我试过了group by,但是它似乎只支持一个方向的聚合。

First the base query 首先基础查询

(from b in EwmBasins
join s in EwmStations on b.BasinId equals s.BasinId into b_s_into
from b_s_from in b_s_into.DefaultIfEmpty()
join bp in EwmBasinPortions on b.BasinId equals bp.BasinId into bp_b_into
from bp_b_from in bp_b_into.DefaultIfEmpty()
join mnb in MonitoringNotificationBasins on bp_b_from.BasinPortionId equals mnb.BasinPortionId into mnb_bp_into
from mnb_bp_from in mnb_bp_into.DefaultIfEmpty()
where b.EwmB118VersionTypeId == EwmB118VersionTypes.Max(m => m.b118VersionTypeId)
group s_from.StationId by b into g
 select new 
 {
  BasinId = g.Key, 
  WellCount = g.ToList().Count(),
  //OrganizationCount = mnb_bp_from.MonitoringNotificatonId.Count() ??? how to do this
 }
)

I expect to see BasinId, # wells and # organizations such as: 我希望看到BasinId,#个井和#个组织,例如:

  1 | 7 | 2
  2 | 2 | 0
  ...

EDIT 编辑

(from basin in EwmBasins
 where basin.EwmB118VersionTypeId == EwmB118VersionTypes.Max(m => m.b118VersionTypeId)
 select new
 {
    basin,
    WellCount = basin.Stations.Count()
 }
).OrderBy(o => o.basin.BasinCode)

It appears to work with @NetMage's suggestion using EF6 navigigational properties. 它似乎可以使用EF6导航属性与@NetMage的建议配合使用。 It does generate a stack of sql statments, however the execution time is no slower than running the query posted in my question so I will go with it. 它确实会生成一堆sql语句,但是执行时间并不比运行在我的问题中发布的查询慢,因此我将继续使用它。

(from b in dbContext.EwmBasins
                                 where b.EwmB118VersionTypeId == dbContext.EwmB118VersionTypes.Max(m => m.b118VersionTypeId)
                                 select new
                                 {
                                     Basin = b,
                                     WellCount = b.Stations.Count(),
                                     OrganizationCount = b.BasinPortions.Count(a => a.MonitoringNotificationBasins.Any(c => c.MonitoringNotification != null))
                                 }
                                ).OrderBy(o => o.Basin.BasinCode)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM