Home > database >  LINQ could not be translated in asp.net
LINQ could not be translated in asp.net

Time:10-16

I'm getting this error.

var query = DbContext.Registries.AsNoTracking().ProjectTo<Registry>(Mapper.ConfigurationProvider);
        if (onlyMine) {
            query = from registry in DbContext.Registries.AsNoTracking()
                    join member in DbContext.RegistryMembers.AsNoTracking() on registry.Id equals member.RegistryId into members
                    from membership in members.DefaultIfEmpty()
                    where onlyMine && membership.MemberId == userId
                    select new Registry {
                        Id = registry.Id,
                        Customer = new Customer {
                            Id = registry.Customer.Id,
                            Name = registry.Customer.Name,
                        },
                        Title = registry.Title,
                        Membership = membership.Role
                    };
        }
        if (!string.IsNullOrWhiteSpace(options.Search)) {
            var term = options.Search.Trim().ToLowerInvariant();
            query = query.Where(x => x.Title.ToLower().Contains(term) ||  x.Customer.Name.ToLower().Contains(term));
        }
        return await query.ToResultSetAsync(options);

and i am getting the following error

System.InvalidOperationException: The LINQ expression 'DbSet .LeftJoin( outer: DbSet, inner: d => EF.Property(d, "CustomerId"), outerKeySelector: d0 => EF.Property(d0, "Id"), innerKeySelector: (o, i) => new TransparentIdentifier<DbRegistry, DbCustomer>( Outer = o, Inner = i )) .OrderBy(d => EF.Property(d.Inner, "Id") == null ? null : new Customer{ Id = d.Inner.Id, Name = d.Inner.Name } .Name)' 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().

CodePudding user response:

var query = DbContext.Registries.AsNoTracking().ProjectTo<Registry>(Mapper.ConfigurationProvider);
    if (onlyMine) {
        query = from registry in DbContext.Registries.AsNoTracking()
                join member in DbContext.RegistryMembers.AsNoTracking() on registry.Id equals member.RegistryId into members
                from membership in members.DefaultIfEmpty()
                where onlyMine && membership.MemberId == userId
                select new Registry {
                    Id = registry.Id,
                    Customer = new Customer {
                        Id = registry.Customer.Id,
                        Name = registry.Customer.Name,
                    },
                    Title = registry.Title,
                    Membership = membership.Role
                };
    } else if (isVendor) {
        query = from registry in DbContext.Registries.AsNoTracking()
                join member in DbContext.RegistryMembers.AsNoTracking() on registry.Id equals member.RegistryId into members
                from membership in members.DefaultIfEmpty()
                select new Registry {
                    Id = registry.Id,
                    Customer = new Customer {
                        Id = registry.Customer.Id,
                        Name = registry.Customer.Name,
                    },
                    Title = registry.Title,
                    Membership = membership.Role
                };
    }
    if (!string.IsNullOrWhiteSpace(options.Search)) {
        var term = options.Search.Trim().ToLowerInvariant();
        query = query.Where(x => x.Title.ToLower().Contains(term) ||  x.Customer.Name.ToLower().Contains(term));
    }
    return await query.ToResultSetAsync(options);
  • Related