Kuttan Sujith
Kuttan Sujith

Reputation: 7979

LINQ to Entities does not recognize the method 'System.String ToString()' method

string[] userIds = userList.Split(','); // is an array of integers
IList<User> users = (from user in this.repository.Users
                     where userIds.Contains(user.Id.ToString())
                     select user).ToList();

the above query gives

System.NotSupportedException: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression

What can I do?

Upvotes: 8

Views: 25660

Answers (2)

Rabih harb
Rabih harb

Reputation: 1382

use can use something like this,

where userIds.Contains(SqlFunctions.StringConvert((double)user.Id))

instead of where userIds.Contains(user.Id.ToString())

this should work

Upvotes: 14

Mark Byers
Mark Byers

Reputation: 837886

Avoid the call to ToString. You want something like this:

userIds.Contains(user.Id)

To make this work the list userIds must be a collection of the type which user.Id has. If you want integers then use int.Parse to convert the strings to integers:

int[] userIds = userList.Split(',').Select(s => int.Parse(s)).ToArray();

Upvotes: 7

Related Questions