c# - 转换 IQueryable<object> 时出现 NotSupportedException

标签 c# linq exception iqueryable

当我这样做时:

    var db = new NotentoolEntities();
    IQueryable<GroupOfBranches> queryGOB = db.tabGroupOfBranches.Cast<GroupOfBranches>().Where(x => x.intGroupID.Equals(ID));
    List<GroupOfBranches> GOB = new List<GroupOfBranches>(queryGOB); //here is the error

我遇到了以下错误:

A first chance exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll

最佳答案

我假设底层 LINQ 提供程序无法将对 Equals 的调用转换为它所知道的任何内容。

改用 == 运算符。这将以不同的表达式树结束,可以翻译。

var db = new NotentoolEntities();
IQueryable<GroupOfBranches> queryGOB =
    db.tabGroupOfBranches.Cast<GroupOfBranches>().Where(x => x.intGroupID == ID));
List<GroupOfBranches> GOB = new List<GroupOfBranches>(queryGOB);

如果 GroupOfBranches 是从 tabGroupOfBranches 返回的任何类型派生的类,那么您应该使用 OfType,而不是 Cast。

否则,Cast 可能会在 Where 之后,并被显式创建 GroupOfBranches 类实例的 Select 替换。

关于c# - 转换 IQueryable<object> 时出现 NotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38122720/

相关文章:

c# - 如何使用 .NET 创建包含多个文件的 7-Zip 存档?

c# - 从无序列表中按值获取有序组列表的最佳方法

c# - 将 Linq 子句添加到 Iqueryable 对象

r - 如何修复错误 "closing unused connection"

java - 使用递归将二叉搜索 TreeMap 存储到数组中并打印出来

c# - Json.net 重命名属性

c# - nhibernate 2本书

c# - 如何在linq中使用if else

c# - 用于选择畅销书的 LINQ 查询

python - 将错误处理中的错误消息与 Salesforce API 隔离