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

các truy vấn trên choLINQ to Entities không nhận ra phương pháp 'System.String ToString()' phương pháp

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

tôi có thể làm gì?

Trả lời

7

Tránh cuộc gọi đến ToString. Bạn muốn một cái gì đó như thế này:

userIds.Contains(user.Id) 

Để làm công việc này trong danh sách userIds phải là một bộ sưu tập các kiểu mà user.Id có. Nếu bạn muốn số nguyên sau đó sử dụng int.Parse để chuyển đổi xâu kí tự sang số nguyên:

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

sử dụng có thể sử dụng một cái gì đó như thế này,

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

thay vì where userIds.Contains(user.Id.ToString())

này nên làm việc

Các vấn đề liên quan