System.NotSupportedException - 无法比较类型为“System.Linq.IQueryable”的元素 [英] System.NotSupportedException - Cannot compare elements of type 'System.Linq.IQueryable

查看:581
本文介绍了System.NotSupportedException - 无法比较类型为“System.Linq.IQueryable”的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在收到以下错误


在EntityFramework.SqlServer.dll中发生了System.NotSupportedException类型的异常,但没有在用户代码中处理

An exception of type 'System.NotSupportedException' occurred in >EntityFramework.SqlServer.dll but was not handled in user code

附加信息:无法比较类型为'System.Linq.IQueryable`1的元素[[System.Int32,mscorlib,Version = 4.0.0.0 ,> Culture = neutral,PublicKeyToken = b77a5c561934e089]]'。只支持基本类型,枚举类型和实体类型。

Additional information: Cannot compare elements of type 'System.Linq.IQueryable`1[[System.Int32, mscorlib, Version=4.0.0.0, >Culture=neutral, PublicKeyToken=b77a5c561934e089]]'. Only primitive types, >enumeration types and entity types are supported.

我的代码是

public List<User> GetActiveUsers(IEnumerable<int> officeIDs, string roleID, string query)
{
    return (from user in GetDBContext.User
            join userRole in GetDBContext.UserRole
                on user.UserID equals userRole.UserID
            join userOffice in GetDBContext.UserAuthorizedOffice
                on user.UserID equals userOffice.UserID
            where user.IsActive == true &&
                    user.UserTypeID == 1 &&
                    userOffice.IsAuthorized &&
                    userOffice.Office.IsActive &&
                    (officeIDs == null || officeIDs.Contains(userOffice.OfficeID)) &&
                    string.Equals(userRole.RoleID, roleID) &&
                    (user.FirstName + user.LastName).Contains(query)
            select user).ToList();
}

这个问题似乎从$

(officeIDs == null || officeIDs.Contains(userOffice.OfficeID))

如果我删除第一个条件, officeIDs == null ,查询执行完美。

If I remove the first condition, officeIDs == null, the query executes perfectly.

任何人都可以解释我丢失的内容,或为什么会出现此错误。

Could anyone please explain what I am missing or as to why this error is thrown.

推荐答案

officeIDs == null 不能覆盖到 sql 语句
更改以下内容:

officeIDs == null can't be coverted to a sql statement Change the below:

(officeIDs == null || officeIDs.Contains(userOffice.OfficeID))

首先检查您的功能顶部是否为空:

First check if it's null at top of your function:

officeIDs = officeIDs ?? Enumerable.Empty<int>;

替换该查询:

(!officeIDs.Any() || officeIDs.Any(id => id == userOffice.OfficeID))

这篇关于System.NotSupportedException - 无法比较类型为“System.Linq.IQueryable”的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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