gpt4 book ai didi

entity-framework - EF Core 以升级后可以翻译的形式重写查询

转载 作者:行者123 更新时间:2023-12-02 18:21:51 28 4
gpt4 key购买 nike

这个查询在我们升级之前工作正常:

var appUsers = await _masterDbContext
.Users
.Include(x => x.UserCustomers)
.AsNoTracking()
.Select(AppUserDto.Projection)
.Where(user => !user.IsDeleted && user.UserCustomers.Any(x => x.CustomerId == tenant.Id && x.UserId == user.Id))
.DistinctBy(x => x.Id)
.ToDataSourceResultAsync(request.GridOptions, cancellationToken: cancellationToken);

我们现在得到错误:

Either rewrite the query in a form that can be translated, 
or switch to client evaluation explicitly by inserting a call to 'AsEnumerable',
'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.

看起来 DistinctBy 是罪魁祸首,但对 LINQ 来说还很陌生,我不知道如何重写它以使其工作。

最佳答案

DistinctBy 更改为 Distinct() 并将其和谓词移动到 Select 之前。我还向上移动了 AsNoTracking():

var appUsers = await _masterDbContext
.Users
.AsNoTracking()
.Include(x => x.UserCustomers)
.Where(user =>
!user.IsDeleted
&& user.UserCustomers
.Any( x => x.CustomerId == tenant.Id ) )
.Distinct()
.Select(AppUserDto.Projection)
.ToDataSourceResultAsync(request.GridOptions, cancellationToken: cancellationToken);

关于entity-framework - EF Core 以升级后可以翻译的形式重写查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70776121/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com