【问题标题】:Cannot compare elements of type 'System.Collections.Generic.List`1[[System.String, mscorlib]]'. Only primitive types, enumeration无法比较“System.Collections.Generic.List`1[[System.String, mscorlib]]”类型的元素。只有原始类型,枚举
【发布时间】:2019-05-09 16:36:25
【问题描述】:

这是我的 EF 查询。

抛出示例:无法比较“System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]”类型的元素。只有原始类型,枚举

所有参数已满。

    public List<User> GetLawyersData(List<Guid> IdList, List<string> selectedCountries, List<string> selectedCities, List<string> selectedDistricts, List<string> selectedOccupations, List<string> selectedProfessions)
    {

        var result = (from u in _userDbContext
                      join ui in _userInformationDbContext
                      on u.Id equals ui.UserID
                      join d in _districtDbContext
                      on ui.DistrictId equals d.Id
                      join c in _cityDbContext
                      on d.CityID equals c.Id
                      join co in _countryDbContext
                      on c.CountryID equals co.Id
                      join uo in _userOccupationInformationDbContext
                      on u.Id equals uo.UserID
                      join oc in _occupationDbContext
                      on uo.OccupationID equals oc.Id
                      join up in _userProfessionDbContext
                      on u.Id equals up.UserID
                      join pr in _professionDbContext
                      on up.ProfessionID equals pr.Id
                      where IdList.Contains(u.Id) &&
                      (selectedCountries != null && selectedCountries.Count > 0 ? selectedCountries.Contains(co.AutoId.ToString()) : true) && (selectedCities != null && selectedCities.Count > 0 ? selectedCities.Contains(c.AutoId.ToString()) : true)
                      && (selectedDistricts != null && selectedDistricts.Count > 0 ?  selectedDistricts.Contains(d.AutoId.ToString()) : true)
                      && (selectedOccupations != null && selectedOccupations.Count > 0 ? selectedOccupations.Contains(oc.AutoId.ToString()) : true)
                      && (selectedProfessions != null && selectedProfessions.Count > 0 ? selectedProfessions.Contains(pr.AutoId.ToString()) : true)
                      select u).ToList();

        return result;

我能做什么?

【问题讨论】:

  • 注释掉where子句的部分,一次一个。当异常停止时,您注释掉的最后一件事就是原因。原因是什么?
  • 需要更多细节。比如,这些_xyzContext 变量是什么?哪个EF版本? (有标签)。为什么你如此大规模地加入而不是使用导航属性?
  • @GertArnold private DbSet _piceAuthorDbContext;私有 DbSet _cityDbContext;私有 DbSet _countryDbContext;私有 DbSet _districtDbContext;私有 DbSet _userProfessionDbContext;私有 DbSet _professionDbContext;
  • @GertArnold 这些在构造函数方法 _piceAuthorDbContext = _context.Set(); _cityDbContext = _context.Set(); _countryDbContext = _context.Set(); _districtDbContext = _context.Set(); _userProfessionDbContext = _context.Set(); _professionDbContext = _context.Set();
  • @GertArnold EF 6.0 版本

标签: c# sql asp.net-mvc entity-framework linq


【解决方案1】:

我认为,问题在于在 linq 查询中使用 ToString() 函数。在反序列化之前,您不能在字段上使用 toString()。因此,与其尝试在查询中调用 ToString(),不如在之后对结果执行此操作或比较确切的数据类型值。

数据库中没有 ToString() 操作的概念,这就是您收到错误的原因。

【讨论】:

  • 不,EF 在查询表达式中支持ToString
  • @GertArnold 它取决于 EF 版本。使用 EF 6.0 的 EF 6.1 及更高版本的提问者正确支持 ToString()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
  • 2018-02-20
  • 2011-05-28
相关资源
最近更新 更多