C#: Net Core 3.1 Contains Linq causes Client Side Exception Error [duplicate]

不羁的心 提交于 2020-07-14 12:06:11

问题


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

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

Does anyone know how to fix?

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

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 throws an exception for Contains

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


回答1:


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() in expression where x is 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);
    }


来源:https://stackoverflow.com/questions/62802339/c-net-core-3-1-contains-linq-causes-client-side-exception-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!