簡體   English   中英

無法在 EF Core 3.1 中翻譯 LINQ 表達式

[英]The LINQ expression could not be translated in EF Core 3.1

我正在嘗試使用 EF Core 3.1 返回任何列中包含指定短語的行。 我收到異常:

InvalidOperationException: LINQ 表達式 'DbSet
.Where(b => __stringProperties_0.Any(y => __Functions_1.Like(
_: y.GetValue( obj: b, index: null).ToString(), matchExpression: __p_2)))' 無法翻譯。 以可以翻譯的形式重寫查詢,或者通過插入對 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的調用來顯式切換到客戶端評估。 有關詳細信息,請參閱https://go.microsoft.com/fwlink/?linkid=2101038

我怎樣才能重寫這個查詢來避免這個異常?

public async Task<List<Table>>
        GetDataAsync(string query)
    {
        var stringProperties = typeof(Table).GetProperties().Where(prop => prop.PropertyType == query.GetType());
        return await _context.Table
             .Where(x => stringProperties
                .Any(y => (EF.Functions.Like(y.GetValue(x, null).ToString(), "%" + query + "%"))))
             .AsNoTracking().ToListAsync();
    }

我認為這個查詢應該這樣做,我在這里使用 EF.Property 方法而不是反射。

return await _context.Table
             .Where(x => stringProperties
                .Any(y => (EF.Functions.Like(EF.Property<string>(x, stringProperties), "%" + query + "%"))))
             .AsNoTracking().ToListAsync();

希望能幫助到你!

var persons = _PersonRepository.TableNoTracking.Where(expression)
                .ProjectTo<PersonSmallInfoDTO>(_mapper.ConfigurationProvider).AsEnumerable();

.AsEnumerable()結束你的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM