【问题标题】:How to implement this sql in entity framework?如何在实体框架中实现这个sql?
【发布时间】:2014-03-02 10:48:19
【问题描述】:

SQL 是:

select 
   a.Id, a.Name,a. ParentId,
   sum(a.departmentCount),
   sum(a.userCount) 
from
   (select 
       d.*,
       0 as departmentCount,
       count(u.Id) as userCount
    from 
       dbo.DepartmentSet d
    left join 
       dbo.UserSet u on d.Id = u.DepartmentId
    where 
       d.ParentId is null
    group by 
       d.Id, d.Name, d.ParentId 

    union all

    select 
       d.*,
       count(d2.Id) as departmentCount,
       0 as userCount
    from 
       dbo.DepartmentSet d
    left join 
       dbo.DepartmentSet d2 on d.Id = d2.ParentId
    where 
       d.ParentId is null
    group by 
       d.Id, d.Name, d.ParentId) a
group by 
    a.Id, a.Name, a.ParentId

【问题讨论】:

    标签: sql linq entity-framework linq-to-entities


    【解决方案1】:

    这是一篇文章,展示了如何进行子查询,这应该可以让您的代码运行

    how to do subquery in LINQ

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      相关资源
      最近更新 更多