2011-07-05 70 views
1

我有類似LINQ加入多個條件或操作

SELECT [t0].[Id], [t1].[Id], [t1].[DepartmentId] 
FROM [ScannedDocuments] AS [t0] 
INNER JOIN [Documents_RelatedDepartments] AS [t1] 
    ON (([t0].[Id] = [t1].[Id]) AND (1 = [t1].[DepartmentId])) 
    OR (([t0].[Id] = [t1].[Id]) AND (56 = [t0].InsertById)) 

結果如預期的SQL服務器上,但我不能把它翻譯到LINQ。有什麼想法嗎?

在此先感謝。

回答

0

看起來這將是東西這樣的:

var query = from doc in context.ScannedDocuments 
      join department in context.RelatedDocuments 
       on doc.Id equals department.Id 
      where doc.InsertById == 56 || department.DepartmentId == 1 
      select new { DocId = doc.Id, DepartmentId = department.Id }; 
+0

雖然其他答案工作正常,這一個很像我的風格;)感謝大家。 – bsaglamtimur

1

試試這個:

from t0 in ctx.ScannedDocuments 
from t1 in ctx.Documents_RelatedDepartments 
where 
(
    (t0.Id == t1.Id) && (t1.Id == 1) || 
    (t0.Id == t1.Id) && (t0.InsertById == 56) 
) 
select new {t0.Id, Id2=t1.Id, t1.DepartmentId} 
0

有一個簡單的查詢。還要注意,Id的限制是通過連接來實現的,因此您不需要在輸出中同時使用這兩個ID。

db.ScannedDocuments 
    .Where(s => s.InsertById == 56) 
    .Join(db.Documents_RelatedDepartments 
      .Where(d => d.DepartmentId == 1), 
     o => o.Id, 
     i => i.Id, 
     (o,i) => new { o.Id, i => i.DepartmentId }) 
+0

謝謝。它的工作是獨立的,但是當我嘗試將它合併到我現有的代碼(它有更多的連接)時,我無法使它工作。 – bsaglamtimur