Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 233
Kuttan Sujith Avatar asked Nov 08 '10 07:11

Kuttan Sujith


2 Answers

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

like image 68
Rabih harb Avatar answered Oct 20 '22 19:10

Rabih harb


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();
like image 7
Mark Byers Avatar answered Oct 20 '22 20:10

Mark Byers