Issue with LINQ Query in Async Method: ‘The LINQ expression could not be translated’

I have an asynchronous method in C# that retrieves a list of users with various nested includes and filters. The method works fine when I don’t pass the arbsResponse parameter, but I encounter an exception when I do.

Here’s the method:

public async Task<List<User>> GetActiveNonDeletedUsersWithValidBookmakersAndFilters(List<ArbsResponse> arbsResponse, List<ValuebetArbsResponse> valuebetResponses)
{
    var users = await _context.Users
        .Where(u => u.TaskIsActive && !u.Deleted)
        .Include(u => u.UsersBookmakers.Where(ub => !ub.Deleted))
            .ThenInclude(ub => ub.UsersFilters.Where(uf => uf.IsActive && !uf.Deleted))
                .ThenInclude(uf => uf.UsersFilterCountries.Where(ufc => !ufc.Deleted))
        .Include(u => u.UsersBookmakers.Where(ub => !ub.Deleted))
            .ThenInclude(ub => ub.UsersFilters.Where(uf => uf.IsActive && !uf.Deleted))
                .ThenInclude(uf => uf.UsersFilterLeagues.Where(ufl => !ufl.Deleted))
        .Include(u => u.UsersBookmakers.Where(ub => !ub.Deleted))
            .ThenInclude(ub => ub.UsersFilters.Where(uf => uf.IsActive && !uf.Deleted))
                .ThenInclude(uf => uf.UsersFilterStrategyTypes.Where(ufst => !ufst.Deleted))
        .Include(u => u.UsersBookmakers.Where(ub => !ub.Deleted))
            .ThenInclude(ub => ub.UsersFilters.Where(uf => uf.IsActive && !uf.Deleted))
                .ThenInclude(uf => uf.UsersFilterBetTypes.Where(ufb => !ufb.Deleted))
        .Include(u => u.UsersBookmakers.Where(ub => !ub.Deleted))
            .ThenInclude(ub => ub.UsersFilters.Where(uf => uf.IsActive && !uf.Deleted))
                .ThenInclude(uf => uf.UsersFilterBetTimings.Where(ufbt => !ufbt.Deleted))
            .Where(u => u.UsersBookmakers.Any(ub => ub.UsersFilters.Any(uf => uf.IsActive && !uf.Deleted &&
    (
        arbsResponse.Any(arb =>
            (arb.MinKoef >= uf.MinimalKoef && arb.MaxKoef <= uf.MaximalKoef) 
        ) ||
        valuebetResponses.Any(valuebet =>
            (valuebet.MinKoef >= uf.MinimalKoef && valuebet.MaxKoef <= uf.MaximalKoef) 
        ))
    ))).ToListAsync();

    return users;
}

When I call this method with the arbsResponse parameter, I get the following exception:

System.InvalidOperationException: 'The LINQ expression 'arb => arb.MinKoef >= EntityShaperExpression: 
    Core.Entities.UsersFilter
    ValueBufferExpression: 
        ProjectionBindingExpression: Inner
    IsNullable: False
.MinimalKoef && arb.MaxKoef <= EntityShaperExpression: 
    Core.Entities.UsersFilter
    ValueBufferExpression: 
        ProjectionBindingExpression: Inner
    IsNullable: False
.MaximalKoef' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'

It seems like the LINQ query can’t be translated into a form that Entity Framework Core can execute on the database side.

When I don’t pass the arbsResponse parameter, the method executes without any issues. Could anyone help me understand why this exception is occurring and how I can modify the query so that it can be translated and executed properly?

Additional Information

Entity Framework Core version: 6.0.32

.NET version:6.0

Clarification:

I would like to clarify that the initial requirement was to perform all operations on the database side. When these operations are performed in-memory, everything works perfectly.

Any advice or suggestions would be greatly appreciated. Thank you!

I tried removing the arbsResponse parameter from the query, and the method executed without any issues. This led me to believe that the problem lies within the part of the query that involves arbsResponse. I also attempted to simplify the query by reducing the number of nested Include and ThenInclude clauses, but the error persisted when arbsResponse was part of the query.

I expected the query to execute and return a list of users who have active, non-deleted tasks and meet the specified filtering criteria based on arbsResponse and valuebetResponses.

New contributor

Lexxxa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

EF Core can’t translate certain LINQ expressions into SQL, especially when it comes to complex nested queries and method calls.

In this case, I think .Where(u => u.UsersBookmakers.Any(ub => .... is probably should be executed separately, more like breaking down the query.

You can try this split (by relying on the client side for the second filtering):

public async Task<List<User>> GetActiveNonDeletedUsersWithValidBookmakersAndFilters(List<ArbsResponse> arbsResponse, List<ValuebetArbsResponse> valuebetResponses)
{
    var users = await _context.Users
        .Where(u => u.TaskIsActive && !u.Deleted)
        .Include(u => u.UsersBookmakers.Where(ub => !ub.Deleted))
            .ThenInclude(ub => ub.UsersFilters.Where(uf => uf.IsActive && !uf.Deleted))
                .ThenInclude(uf => uf.UsersFilterCountries.Where(ufc => !ufc.Deleted))
        .Include(u => u.UsersBookmakers.Where(ub => !ub.Deleted))
            .ThenInclude(ub => ub.UsersFilters.Where(uf => uf.IsActive && !uf.Deleted))
                .ThenInclude(uf => uf.UsersFilterLeagues.Where(ufl => !ufl.Deleted))
        .Include(u => u.UsersBookmakers.Where(ub => !ub.Deleted))
            .ThenInclude(ub => ub.UsersFilters.Where(uf => uf.IsActive && !uf.Deleted))
                .ThenInclude(uf => uf.UsersFilterStrategyTypes.Where(ufst => !ufst.Deleted))
        .Include(u => u.UsersBookmakers.Where(ub => !ub.Deleted))
            .ThenInclude(ub => ub.UsersFilters.Where(uf => uf.IsActive && !uf.Deleted))
                .ThenInclude(uf => uf.UsersFilterBetTypes.Where(ufb => !ufb.Deleted))
        .Include(u => u.UsersBookmakers.Where(ub => !ub.Deleted))
            .ThenInclude(ub => ub.UsersFilters.Where(uf => uf.IsActive && !uf.Deleted))
                .ThenInclude(uf => uf.UsersFilterBetTimings.Where(ufbt => !ufbt.Deleted))
        .ToListAsync();

    var filteredUsers = users.Where(u =>
        u.UsersBookmakers.Any(ub => ub.UsersFilters.Any(uf => uf.IsActive && !uf.Deleted &&
            (
                arbsResponse.Any(arb =>
                    (arb.MinKoef >= uf.MinimalKoef && arb.MaxKoef <= uf.MaximalKoef)
                ) ||
                valuebetResponses.Any(valuebet =>
                    (valuebet.MinKoef >= uf.MinimalKoef && valuebet.MaxKoef <= uf.MaximalKoef)
                )
            )
        ))
    ).ToList();

    return filteredUsers;
}

First try if this solves the issue first, but bare in mind that if the dataset is large, loading it into memory for the second filter can have some performance impact.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật