简体   繁体   English

包含不能翻译 EF .net 核心 3.1

[英]contains cannot not be translate EF .net core 3.1

Hello I try to make a query on EF 3.1 like this:您好,我尝试像这样对 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();

But the transaltion fail because this predicate但是转换失败是因为这个谓词

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

And the error is错误是

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.

How can fix it please?请问怎么解决? Regards问候

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()以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估

The problem is mentioned in the explanation, this kind of queries requires client side evaluation, which is off by default.问题在解释中提到,这种查询需要客户端评估,默认关闭。 This type of evaluation is not supported on server side because it needs to load the entities into the memory and filter a large amount of data and that could result in bad performance.服务器端不支持这种类型的评估,因为它需要将实体加载到 memory 并过滤大量数据,这可能会导致性能不佳。 For more details see Unsupported client evaluation .有关更多详细信息,请参阅不支持的客户端评估

Actually, I went through this problem and found a better approach using a third party nuget as suggested by Microsoft docs .实际上,我经历了这个问题,并按照Microsoft docs 的建议使用第三方 nuget 找到了更好的方法。 Download and install LinqKit , then you can use the dynamic composing expression predicates :下载并安装LinqKit ,然后您可以使用动态组合表达式谓词

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 3.1 - EF Core 无法翻译 GroupBy 和 Join - ASP.NET Core 3.1 - EF Core can't translate GroupBy and Join EF Core 3.1 为包含引发异常 - EF Core 3.1 throws an exception for Contains 在 EF Core 2 中使用 Contains as (NOT) EXIST 不再适用于 EF Core 3.1 - Using Contains as (NOT) EXIST in EF Core 2 no longer works in EF Core 3.1 EF Core 3.1 在查询时不使用辅助类进行转换 - EF Core 3.1 doesn't translate with a helper class on querying EF Core 3.1:Sequence 不包含带有 FromSqlRaw 查询存储过程的元素 - EF Core 3.1: Sequence contains no elements with FromSqlRaw query a stored procedure EF Core 3.1 不允许对枚举属性进行包含搜索 - EF Core 3.1 does not allow Contains search on Enum property ASP.NET 核心 (3.1) 与 EF 核心迁移和 IHttpContextAccessor - ASP.NET Core (3.1) with EF Core Migrations and IHttpContextAccessor 如何在没有 EF Core 的情况下在 ASP .NET Core 3.1 中使用 Identity? - How to use Identity in ASP .NET Core 3.1 without EF Core? .NET Core 3.1 Linq 和 SQL 服务器无法转换 DateTime - .NET Core 3.1 Linq and SQL Server can not translate DateTime .net core 3.1 无法删除 cookie - .net core 3.1 cannot delete cookies
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM