【问题标题】:System.NotSupportedException - Cannot compare elements of type 'System.Linq.IQueryableSystem.NotSupportedException - 无法比较“System.Linq.IQueryable”类型的元素
【发布时间】:2017-01-24 07:05:58
【问题描述】:

我目前收到以下错误

“System.NotSupportedException”类型的异常发生在 >EntityFramework.SqlServer.dll 但未在用户代码中处理

附加信息:无法比较“System.Linq.IQueryable`1[[System.Int32, mscorlib, Version=4.0.0.0, >Culture=neutral, PublicKeyToken=b77a5c561934e089]]”类型的元素。仅支持原始类型、>枚举类型和实体类型。

我的代码是

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,查询将完美执行。

谁能解释一下我缺少什么或者为什么会抛出这个错误。

【问题讨论】:

    标签: c# .net entity-framework linq


    【解决方案1】:

    officeIDs == null 不能转换为 sql 语句 更改以下内容:

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

    首先检查函数顶部是否为空:在后面添加括号

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

    以及该查询的替换:

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

    【讨论】:

      【解决方案2】:

      officeIDs == null 是一个语句,它不应该是linq 的一部分,因为它不能映射到数据库查询(不是查询)。

      确保 officeID 不为空,并将其从 linq 查询中删除。

      public List<User> GetActiveUsers(IEnumerable<int> officeIDs, string roleID, string query)
          {
              officeIDs = officeIDs ?? new List<int>();
      
              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.Contains(userOffice.OfficeID) &&
                              string.Equals(userRole.RoleID, roleID) &&
                              (user.FirstName + user.LastName).Contains(query)
                      select user).ToList();
          }
      

      【讨论】:

      • 现在,如果它为 null,则不会返回任何内容。相反,你应该做类似query = from........(没有ToList)然后if officeIDs is not null { query.Where.... } return query
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 2019-06-25
      • 1970-01-01
      相关资源
      最近更新 更多