Tania Marinova
Tania Marinova

Reputation: 1898

LINQ - to Entities doesn't recognize the method system.string ToString() exception

I've got the above ring exception in my code. What I try is to use where clause with a list variable and that's why I use Contains method but I keep getting the error and I can't understand what I do wrong

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;
}

Upvotes: 1

Views: 204

Answers (3)

Gustavo Gondim
Gustavo Gondim

Reputation: 1643

The ToString() method is not evaluated by LinQ to Entities, because it cannot be translated to SQL.

Try to change the type of your list to the destination type or parse them into a new List.

Upvotes: 1

Khan
Khan

Reputation: 18142

You cannot use ToString or any other methods that cannot be translated to SQL.

You should be able to do the following:

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();

Upvotes: 2

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26717

you cannot use .ToString() inside a LinqToSql expression because it will try to translate it to SQL

Upvotes: 3

Related Questions