Skip to main content
 首页 » 编程设计

c#之转换 IQueryableobject> 时出现 NotSupportedException

2024年02月05日34java哥

当我这样做时:

    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 替换。