2012-01-05 74 views
0

我想動態地構建LINQ where語句,但我在嘗試使用列表中的contains方法編寫IN語句時遇到問題。使用Contains在LINQ子句中使用實體框架執行IN語句

這裏是我的代碼:

IQueryable<Customer> customers = 
       (from cus in objectCustomer 
       select cus); \\objectCustomer is all customers 

//List of customer ID's passed into the method. 
string[] sCustomerIDs = (string[])DictionaryParameters["CustomerIDs"]; 
customers = customers.Where(c => c.Assets.Any(a => sAssetIDs.Contains(a.UN_Asset.ToString()))); 

return customers.Cast<T>().ToList(); 

當返回線運行我收到以下錯誤: LINQ到實體無法識別方法「System.String的ToString()」方法,而這種方法無法翻譯成商店表達。

任何想法?如果我將IQueryable更改爲IEnumerable以查詢運行,但我需要使用IQueryable,因爲我正在使用實體框架,並且IEnumerable會在我的過濾器發生之前返回所有記錄。

很多謝謝。

回答

2

請試試這個:

string[] sCustomerIDs = (string[])DictionaryParameters["CustomerIDs"];  

IQueryable<Customer> customers = 
      (from cus in objectCustomer 
      where sCustomerIDs.Contains(cus.*[Your Field]*) 
      select cus);