简体   繁体   English

ASP.NET 核心 Web API - System.InvalidOperationException: LINQ 表达式 'DbSet<mandate> ()</mandate>

[英]ASP.NET Core Web API - System.InvalidOperationException: The LINQ expression 'DbSet<Mandate>()

In ASP.NET Core-6 Web API Project I have this code:在 ASP.NET Core-6 Web API 项目中我有这段代码:

private async Task<int> GetTotalCustomers()
{
    var userName = _currentUserService.UserName;

    var merchantId = _dbContext.Merchants
        .Where(u => u.User.UserName == userName)
        .Select(m => m.Id)
        .FirstOrDefault();

    var customersList = await _dbContext.Mandates
        .Where(x => x.MerchantId == merchantId)
        .GroupBy(x => x.DrAccountNumber)
        .ToListAsync();

    return customersList.Count();
}

I got this error:我收到此错误:

System.InvalidOperationException: The LINQ expression 'DbSet().Where(x => x.MerchantId == __merchantId_0).GroupBy(x => x.DrAccountNumber)' could not be translated. System.InvalidOperationException:无法翻译 LINQ 表达式“DbSet().Where(x => x.MerchantId == __merchantId_0).GroupBy(x => x.DrAccountNumber)”。 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'.以可翻译的形式重写查询,或通过插入对“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或“ToListAsync”的调用显式切换到客户端评估。

As stated in the error, already I have ToListAsync()如错误中所述,我已经有ToListAsync()

How do I get this resolved?我该如何解决这个问题?

Thanks谢谢

Instead of executing two queries, everything can be written in one query, which will be more effective.不是执行两个查询,而是可以将所有内容写在一个查询中,这样会更有效。

private Task<int> GetTotalCustomers()
{
    var userName = _currentUserService.UserName;

    var query =
        from m in _dbContext.Merchants.Where(m => m.User.UserName == userName)
        from mandate in _dbContext.Mandates.Where(mandate => mandate.MerchantId == m.Id)
        select x.DrAccountNumber;

    return query.Distinct().CountAsync();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心 API System.InvalidOperationException 在本地化 - ASP.NET Core API System.InvalidOperationException in localization ASP.NET Core 6 Web API 的集成测试抛出 System.InvalidOperationException - Integration test for ASP.NET Core 6 web API throws System.InvalidOperationException System.InvalidOperationException:无法解析 ASP.NET Core 中的服务 - System.InvalidOperationException: Unable to resolve service in ASP.NET Core ASP.Net 错误 System.InvalidOperationException - ASP.Net error System.InvalidOperationException System.InvalidOperationException:LINQ 表达式....'无法翻译。 ASP.NET 6...方法“DecodeFrom64”的翻译失败 - System.InvalidOperationException: The LINQ expression.... 'could not be translated. ASP.NET 6 ...Translation of method 'DecodeFrom64' failed System.InvalidOperationException:LINQ 表达式 - System.InvalidOperationException: The LINQ expression Asp.net 核心System.InvalidOperationException: 无法追踪实体类型x的实例 - Asp.net core System.InvalidOperationException: The instance of entity type x cannot be tracked ASP.NET 核心错误:System.InvalidOperationException:尝试激活时无法解析服务类型 - ASP.NET Core error: System.InvalidOperationException: Unable to resolve service for type while attempting to activate System.InvalidOperationException:idp 声明丢失身份服务器 4.1.1 Asp.net 核心 3 - System.InvalidOperationException: idp claim is missing Identity server 4.1.1 Asp.net core 3 ASP.NET 核心 - System.InvalidOperationException:尝试激活时无法解析服务类型 - ASP.NET Core - System.InvalidOperationException: Unable to resolve service for type while attempting to activate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM