2010-11-08 129 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())

这应该工作