嘗試按 customer.name 或 customer.legalname 排序時出現此錯誤。但如果我按標題排序,它可以作業
public async Task<ActionResult<ResultSet<Registry>>>
GetRegistries([FromQuery]ListOptions<RegistryFilter> options = null) {
var query = DbContext.Registries.AsNoTracking()
query = select new Registry {
Id = registry.Id,
Alias = registry.Alias,
Customer = new Customer {
Id = registry.Customer.Id,
LegalName = registry.Customer.LegalName,
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.LegalName.ToLower().Contains(term) ||
x.Customer.Name.ToLower().Contains(term));
}
return await query.ToResultSetAsync(options);
我收到以下錯誤
System.InvalidOperationException:LINQ 運算式'DbSet .LeftJoin(外部:DbSet,內部:d => EF.Property<Nullable>(d,“CustomerId”),outerKeySelector:d0 => EF.Property<Nullable>(d0,“ Id"), innerKeySelector: (o, i) => new TransparentIdentifier<DbRegistry, DbCustomer>( Outer = o, Inner = i )) .OrderBy(d => EF.Property<Nullable>(d.Inner, "Id" ) == null ? null : new Customer{ Id = d.Inner.Id, LegalName = d.Inner.LegalName, Name = d.Inner.Name } .Name)' 無法翻譯。以可翻譯的形式重寫查詢,或通過插入對 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的呼叫顯式切換到客戶端評估。
uj5u.com熱心網友回復:
嘗試在投影之前應用過濾。沿著這條線的東西:
query = DbContext.Registries.AsNoTracking()
if (!string.IsNullOrWhiteSpace(options.Search)) {
var term = options.Search.Trim().ToLowerInvariant();
query = query.Where(x =>
x.Title.ToLower().Contains(term) ||
x.Customer.LegalName.ToLower().Contains(term) ||
x.Customer.Name.ToLower().Contains(term));
}
return await query
.Select(registry => new Registry
{
Id = registry.Id,
Alias = registry.Alias,
Customer = new Customer
{
Id = registry.Customer.Id,
LegalName = registry.Customer.LegalName,
Name = registry.Customer.Name,
Notes = registry.Customer.Notes
},
Title = registry.Title,
Schema = registry.Schema
})
.ToResultSetAsync(options);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/515617.html
標籤:网林克
