在我看來,它的問題您是否使用臨時儲存一個IQueryable或不是一個變量給出NotSupportedException異常。參見簡化下面的例子:LINQ到SQL使用本地變量時
這工作:
List<string> jobNames = new List<string> { "ICT" };
var ictPeops = from p in dataContext.Persons
where ( from j in dataContext.Jobs
where jobNames.Contains(j.Name)
select j.ID).Contains(p.JobID)
select p;
但是當我使用臨時存儲變量子查詢我得到一個異常:
List<string> jobNames = new List<string> { "ICT" };
var jobs = from j in dataContext.Jobs
where jobNames.Contains(j.Name)
select j.ID;
var ictPeops = from p in dataContext.Persons
where jobs.Contains(p.JobID)
select p;
「System.NotSupportedException:查詢 與當地的集合都不會 支持」
我看不出有什麼問題。這是不是應該在LINQ中工作的邏輯?
UPDATE: 昨天我找到了解決方法,同時使用多個變量來獲得1個查詢:
var jobs = from j in dataContext.Jobs
where jobNames.Contains(j.Name)
select j.ID;
var ictPeops = from p in dataContext.Persons
join j in jobs on p.JobID equals j
select p;
但我仍然很困惑。任何人都可以闡明爲什麼第一個查詢在使用變量時不起作用?
我無法重現。請將您的Linq上傳到SQL架構併發送給我們鏈接 – Andrey