假设我有计划和文件
Dim myPlans = _context.Plans.Where(predicate1)
Dim myDocuments = _context.Documents.Where(predicate2)
我已经使用 PredicateBuilder 为每个结构构建了 where 子句。因此,myPlans 和 myDocuments 具有正确的 SQL 语句。
我想做的是将这两个表连接到一个 linq 语句中。我遇到的问题是,默认情况下 AND 条件加入 where 子句。
myPlans Where 子句 : (p.name like "%test%"AND p.name like "%bed%") OR (p.description like "%test%"AND p.description like "%bed%")
myDocuments Where 子句 : (d.name like "%test%"AND d.name like "%bed%") OR (d.description like "%test%"AND d.description like "%bed%")
当我将两者结合起来时,期望的结果 Where 子句是:
其中 (d.ID = p.ID) AND
(上面的 myplans where 子句)或(上面的 mydocument where 子句)。意思是,我希望每个表中的两个 where 子句都是“or”而不是“And”。
where 子句的当前结果是:
其中 (d.ID = p.ID) AND
(上面的 myplans where 子句)AND(上面的 mydocument where 子句)。意思是,我希望每个表中的两个 where 子句都是“or”而不是“And”。
我正在形成这样的声明:
Dim test = From d in myDocuments _
Join p in MyPlans on d.ID Equals p.ID _
Select d.Name, p.Name
最佳答案
为了达到您想要的结果,您需要在单个语句中进行谓词过滤和连接。
Dim myCriteria() = {"test", "bed"}
Dim test = from d in _context.Documents _
join p in _context.Plans on d.ID Equals p.ID _
where (myCriteria.Contains(d.Name) OR _
myCriteria.Contains(d.Descrition)) _
OR (myCriteria.Contains(p.Name) OR _
myCriteria.Contains(p.Description)) _
select Document = d.Name, Plan = p.Name
关于vb.net - LINQ to SQL 并使用 OR 子句连接两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/783742/