2013-09-25 94 views
1

我已經在我的代碼中有上述的環異常。我嘗試是使用where子句用一個列表變量,這就是爲什麼我使用包含的方法,但我不斷收到錯誤,我不明白我做錯了LINQ-到實體無法識別的方法system.string ToString()異常

List<string> TouristID = (List<string>)Session["TouristID"]; 
    List<Tourist> customerInterests = (from tourist in db2.Tourist 
          where (TouristID.Contains(tourist.Tourist_ID.ToString())) 
          select tourist).ToList(); 

foreach (var customer in customerInterests) 
{ 
    String strName_kir = customer.Name_kir; 
    String Lastname_kir = customer.Midname_kir; 
} 
+0

確切的錯誤是什麼? –

+0

讓你的'列表'列出一個'列表'然後放掉'.ToString()'調用,你應該沒問題。 –

+0

謝謝安德魯 - 我已經這樣做了:) –

回答

1

ToString()方法不受LinQ在評價實體,因爲它不能被轉換爲SQL。

嘗試將列表的類型更改爲目標類型或將它們解析爲新列表。

3

不能使用.ToString()一個LinqToSql表達式中因爲它會嘗試將其轉換爲SQL

+0

謝謝!我很困,甚至看不到我在寫什麼。再次感謝 –

2

您不能使用ToString或任何其他無法轉換爲SQL的方法。

你應該能夠做到以下幾點:

List<string> TouristID = (List<string>)Session["TouristID"]; 

//Get them as integers 
var touristIds = TouristID.Select(x => int.Parse(x)); 

List<Tourist> customerInterests = (from tourist in db2.Tourist 
         where (touristIds.Contains(tourist.Tourist_ID)) 
         select tourist).ToList(); 
相關問題