2011-04-07 36 views
1

您是否知道val引用此查詢時爲此查詢中的共享選項ID做了什麼?按整數值的引用覆蓋我的linq查詢

下面是代碼:

lst = new List<IQueryable<tblProduct>>(); 
int choiceID = 30; 
lst.Add(from t in originalQuery 
    where t.tblProductChoiceTag.Any(c => c.ChoiceID == choiceID) 
    select t); 
choiceID = 31; 
lst.Add(from t in originalQuery 
    where t.tblProductChoiceTag.Any(c => c.ChoiceID == choiceID) 
    select t); 

IQueryable<tblProduct> q = null; 

bool first = true; 
foreach (var tquery in lst) 
{ 
    if (first) 
    { 
     q = tquery; 
     first = false; 
    } 
    else 
    { //the next one combine it 
     q = q.Union(tquery); 
    } 
} 

回答

1

你拍攝的choiceID變量。請記住,(a)查詢執行被延遲,(b)關閉捕獲變量,而不是值。在你的情況下,你基本上想要爲每個查詢使用一個不同的變量,或者簡單地用變量代表的數字代碼。