2013-03-29 57 views
0

Linq是全新的,所以我很抱歉,如果這真的很愚蠢。LINQ - 從選擇加入沒有組通過

我想從一個多表連接與where子句,沒有組通過。我已經看過一些小組的例子,如果需要的話,會採取這種方式,但我想知道是否有辦法避免它。是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') 

是什麼讓這個最好的方法是什麼?

+0

Linq to? (實體/ SQL)。你有什麼導航屬性?你可能不需要在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(); 
+0

輝煌。謝謝,那工作得很好。 – John