LINQ to Entities无法识别方法'System.String ToString()'方法 [英] LINQ to Entities does not recognize the method 'System.String ToString()' method

查看:354
本文介绍了LINQ to Entities无法识别方法'System.String ToString()'方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();

上面的查询给出

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

我该怎么办?

推荐答案

避免调用ToString.您想要这样的东西:

Avoid the call to ToString. You want something like this:

userIds.Contains(user.Id)

要使此工作有效,列表userIds必须是user.Id具有的类型的集合.如果要整数,请使用int.Parse将字符串转换为整数:

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();

这篇关于LINQ to Entities无法识别方法'System.String ToString()'方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆