2010-11-08 133 views
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(); 

上面的查詢給LINQ到實體無法識別方法 'System.String的ToString()' 方法

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

我能做些什麼?

回答

7

避免撥打ToString。你想是這樣的:

userIds.Contains(user.Id) 

爲了使這項工作列表userIds必須是user.Id具有類型的集合。如果你想整數然後使用int.Parse到字符串轉換爲整數:

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

使用可以使用這樣的事情,

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

,而不是where userIds.Contains(user.Id.ToString())

這應該工作