我目前收到以下错误
> EntityFramework.SqlServer.dll中出现"System.NotSupportedException"类型的异常,但未在用户代码中处理
附加信息:无法比较'System.Linq.IQueryable`1 [[System.Int32,mscorlib,Version = 4.0.0.0,> Culture = neutral,PublicKeyToken = b77a5c561934e089]]'类型的元素.仅支持基本类型,>枚举类型和实体类型.
我的代码是
public ListGetActiveUsers(IEnumerable 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
查询将完美执行.
任何人都可以解释我错过了什么或为什么抛出这个错误.
officeIDs == null
不能转换为sql
声明更改如下:
(officeIDs == null || officeIDs.Contains(userOffice.OfficeID))
首先检查函数顶部是否为空:
officeIDs = officeIDs ?? Enumerable.Empty;
并替换该查询:
(!officeIDs.Any() || officeIDs.Any(id => id == userOffice.OfficeID))