LINQ - 来自select with join的计数,没有group by

时间:2013-03-29 10:49:22

标签: linq

Linq对我来说是全新的,所以如果这真的很蠢,我会道歉。

我试图通过where子句从多表连接中获取计数,而不使用group by。我已经看到了分组的例子,并且如果需要的话会诉诸于此,但我想知道是否有办法避免它。 sql我的查询看起来像这样;

 
 SELECT Count(*)
FROM   plans p
       JOIN organizations o
         ON p.org_id = o.org_id
            AND o.deleted IS NULL
       JOIN orgdata od
         ON od.org_id = o.org_id
            AND od.active = 1
       JOIN orgsys os
         ON os.sys_id = od.sys_id
            AND os.deleted IS NULL
WHERE  p.deleted IS NULL
       AND os.name NOT IN ( 'xxxx', 'yyyy', 'zzzz' ) 

获得此功能的最佳方式是什么?

1 个答案:

答案 0 :(得分:1)

您只需拨打Count()即可。您计算结果数量。如下所示:

var names = new[] { "xxxx", "yyyy", "zzzz" };
var query = from plan in db.Plans
            where plan.Deleted == null
            join organization in db.Organizations
              on plan.OrganizationId equals organization.OrganizationId
            where organization.Deleted == null
            join orgData in db.OrganizationData
              on organization.OrganizationId equals orgData.OrganizationId
            where orgData.Active == 1
            join os on db.OrganizationSystems
              on orgData.SystemId equals os.SystemId
            where os.Deleted == null &&
                  !names.Contains(os.Name)
            select 1; // It doesn't matter what you select here
 var count = query.Count();