C#:Net Core 3.1包含Linq导致客户端异常错误 [英] C#: Net Core 3.1 Contains Linq causes Client Side Exception Error

查看:556
本文介绍了C#:Net Core 3.1包含Linq导致客户端异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容在Net Core 3.1中引发了客户端异常错误

The following is throwing a Client Side Exception error in Net Core 3.1

不确定为什么,PropertyIdentifier位于属性实体数据"表和类中.

Not sure why, PropertyIdentifier is in Property Entity Data table and class.

有人知道如何解决吗?

  public async Task<IEnumerable<PropertyDto>> GetByPropertyIdentifier(List<string> identifiers)
    {
        var properties = await _dataContext.Property
            .Where(x => identifiers.Contains(x.PropertyIdentifier))
            .ToListAsync();

        return properties.Select(_mapper.Map);
    }

错误:错误:"无效请求:LINQ表达式'DbSet .where(p => __identifiers_0.Contains(p.PropertyIdentifier))'无法翻译.可以以可翻译的形式重写查询,也可以通过插入对AsEnumerable(),AsAsyncEnumerable(),ToList()或ToListAsync()的调用来显式切换到客户端评估.

Error: error: "Invalid request: The LINQ expression 'DbSet .Where(p => __identifiers_0.Contains(p.PropertyIdentifier))' 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().

资源:(但是没有被调用) *由于查询量大,因此也不想强制客户端评估的解决方案

Resource: (No distinct is being called however) *Also do not want solution which forces client side evaluation, since its large query

EF Core 3.1为Contains引发了异常

使用Net Core 3.1

Using Net Core 3.1

public partial class Property
{
    public Property()
    {
        AddressChangeRequest = new HashSet<AddressChangeRequest>();
        CalamityEventHistory = new HashSet<CalamityEventHistory>();
        CutHistoryPropertyChildProperty = new HashSet<CutHistoryProperty>();
       ....}

    public int PropertyId { get; set; }
    public int? LkPropertyTypeId { get; set; }
    public int? LkZoningId { get; set; 
    public string PropertyIdentifier { get; set; }
    ....


 modelBuilder.Entity<Property>(entity =>
        {
            entity.ToTable("Property", "PM");

            entity.HasIndex(e => e.PropertyIdentifier)
                .HasName("Unique_Property_PropertyIdentifier")
                .IsUnique();

            entity.Property(e => e.PropertyIdentifier)
                .HasMaxLength(16)
                .IsUnicode(false);

推荐答案

请将标识符更改为可枚举.下面的资源不正确.将其转换为Enumerable以供标识符使用是可行的.投射到列表对我不起作用.

Please Change Identifiers to Enumerable. This resource below is incorrect. Converting it to Enumerable for identifiers works. Casting to List does not work for me.

EF Core 3 x.Contains( ),其中x是ICollection

    public async Task<IEnumerable<PropertyDto>> GetByPropertyIdentifier(List<string> identifiers)
    {
        var identifiersEnumerable = identifiers.AsEnumerable();
        var properties = await _dataContext.Property
            .Where(x => identifiersEnumerable.Contains(x.PropertyIdentifier))
            .ToListAsync();

        return properties.Select(_mapper.Map);
    }

这篇关于C#:Net Core 3.1包含Linq导致客户端异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆