【问题标题】:contains cannot not be translate EF .net core 3.1包含不能翻译 EF .net core 3.1
【发布时间】:2021-03-15 12:04:34
【问题描述】:

您好,我尝试像这样在 EF 3.1 上进行查询:

  Expression<Func<MedicalResponsibleWard, bool>> predicate = x => x.InstitutionId == request.InstitutionId;
        if (request.GroupId != null)
            predicate = predicate.AndAlso(x => x.GroupId == request.GroupId);
        if (request.Code != null)
            predicate = predicate.AndAlso(x => x.Code == request.Code);
       
            if (request.MedicalResponsibleWarName != null)
                predicate = predicate.AndAlso(x => x.Properties.Any(m => m.Name.Contains(request.MedicalResponsibleWarName, StringComparison.InvariantCultureIgnoreCase)));

        return (await _genericRepository.GetAsync(predicate, query => query.Include(c => c.Properties)))
         .OrderBy(g => g.Id).ToList();

但是由于这个谓词导致转换失败

if (request.MedicalResponsibleWarName != null)
            predicate = predicate.AndAlso(x => x.Properties.Any(m => m.Name.Contains(request.MedicalResponsibleWarName, StringComparison.InvariantCultureIgnoreCase)));

错误是

The LINQ expression 'DbSet<MedicalResponsibleWardProperty>
.Where(m0 => EF.Property<Nullable<int>>((EntityShaperExpression: 
    EntityType: MedicalResponsibleWard
    ValueBufferExpression: 
        (ProjectionBindingExpression: EmptyProjectionMember)
    IsNullable: False
), "Id") != null && EF.Property<Nullable<int>>((EntityShaperExpression: 
    EntityType: MedicalResponsibleWard
    ValueBufferExpression: 
        (ProjectionBindingExpression: EmptyProjectionMember)
    IsNullable: False
), "Id") == EF.Property<Nullable<int>>(m0, "MedicalResponsibleWardId"))
.Any(m0 => m0.Name.Contains(
    value: __request_MedicalResponsibleWarName_1, 
    comparisonType: InvariantCultureIgnoreCase))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

请问如何解决? 问候

【问题讨论】:

    标签: c# postgresql entity-framework asp.net-core


    【解决方案1】:

    要么以可翻译的形式重写查询,要么通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估

    解释中提到了问题,这种查询需要客户端评估,默认关闭。服务器端不支持这种类型的评估,因为它需要将实体加载到内存中并过滤大量数据,这可能会导致性能不佳。更多详情请见Unsupported client evaluation

    实际上,我解决了这个问题,并找到了一个更好的方法,使用第三方 nuget 作为suggested by Microsoft docs。下载安装LinqKit,即可使用dynamic composing expression predicates

    IQueryable<Product> SearchProducts (params string[] keywords)
    {
      var predicate = PredicateBuilder.False<Product>();
    
      foreach (string keyword in keywords)
        predicate = predicate.Or (p => p.Description.Contains (keyword));
    
      return dataContext.Products.Where (predicate);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 2020-10-19
      相关资源
      最近更新 更多