LINQ使用OR运算符加入多个条件

时间:2011-07-05 12:06:32

标签: .net 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 server上按预期结果但我无法将其转换为Linq。有什么想法吗?

提前致谢。

3 个答案:

答案 0 :(得分: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}

答案 1 :(得分: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 };

答案 2 :(得分:0)

有一个更简单的查询。另请注意,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 } )