8
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?

Gabe
  • 84,912
  • 12
  • 139
  • 238
Kuttan Sujith
  • 7,889
  • 18
  • 64
  • 95

2 Answers2

14

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

Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51
Rabih harb
  • 1,372
  • 12
  • 11
7

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();
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452