[英]How to use predicate builder with linq2sql and OR operator
我有兩個表(TABLE1,TABLE2 - 我知道的唯一),它們分別具有1對多關系,並且兩個表的ID列之間有一個外鍵。
使用linq2sql我試圖選擇所有TABLE1條目,使其相應的TABLE2值包含我傳遞它的列表中的至少1項。
這是我在LINQPad (真棒程序)中使用的一些示例代碼,用於測試它但是得到錯誤NotSupportedException:用於查詢運算符'Any'的不支持的重載。
long[] items = { 3, 5, 8 };
var predicate = PredicateBuilder.False<TABLE2>();
foreach (long i in items)
{
long t = i;
predicate = predicate.Or(att => att.ID == t);
}
//TABLE2.Where(predicate).Dump(); //works like a charm
IQueryable query =
from t1 in TABLE1
where t1.TABLE2.AsQueryable().Any(predicate) //problem with this line
select a;
query.Dump();
UPDATE
在LinqPad中使用LinqKit時,添加對LinqKit.dll的引用,取消選中Include PredicateBuilder,然后在Additional Namespace Imports選項卡下添加LinqKit。
解決方法是
TABLE1
對象上調用AsExpandable() 所以你的最終查詢是
IQueryable query =
from t1 in TABLE1.AsExpandable()
where t1.TABLE2.Any(predicate.Compile()) //the problem should disappear
select a;
更多信息在這里 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.