【问题标题】:LINQ - count from select with join with no group byLINQ - 从选择中计数,加入没有分组依据
【发布时间】:2013-03-29 10:49:22
【问题描述】:

Linq 对我来说是全新的,所以如果这真的很愚蠢,我深表歉意。

我正在尝试从带有 where 子句的多表连接中获取计数,而不使用 group by。我已经看到了 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' ) 

最好的方法是什么?

【问题讨论】:

  • Linq to? (实体/sql)。你有什么导航属性?您可能不需要 linq 中的 join 语句。

标签: linq


【解决方案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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 2010-10-16
    相关资源
    最近更新 更多