[英]Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly… by inserting a call to 'AsEnumerable
I am developing a net 5 api and am trying to implement Iqueryable while querying the db.我正在开发一个 net 5 api,并试图在查询数据库时实现 Iqueryable。 I understand the fundamental difference between IQueryable and IEnumerable.
我了解 IQueryable 和 IEnumerable 之间的根本区别。 Everything works ok until I try to implement search functionality, then I get the aforementioned message in Postman: 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.
一切正常,直到我尝试实现搜索功能,然后我在 Postman 中收到上述消息:要么以可以翻译的形式重写查询,要么通过插入对“AsEnumerable”、“AsAsyncEnumerable”的调用来显式切换到客户端评估、“ToList”或“ToListAsync”。 Here is my code, any help is appreciated, feel free to point me to every mistake in my code:
这是我的代码,感谢任何帮助,请随时指出我代码中的每个错误:
Services :服务:
public async Task<IEnumerable<TransactionsForUserVM>>
ShowTransactionsForSpecificUser2(QueryParameters queryParameters,
string email)
{
IQueryable<TransactionsForUserVM> transactions =
(from t in _context.StockTransactions
join u in _context.Users.Where(u => u.Email == email)
on t.Email equals u.Email
join s in _context.Stocks
on t.StockId equals s.Id
select new TransactionsForUserVM
{
Id = t.Id,
StockId = s.Id,
Stock = s.Symbol,
Quantity = t.Quantity,
Purchase = t.Purchase,
Price = t.Price,
Resolved = t.Resolved,
Date = t.Date,
Email = email
}).AsQueryable().OrderBy(s => s.Date);
if (queryParameters.HasQuery())
{
transactions = transactions
.Where(t => t.Stock.ToLowerInvariant().Contains(queryParameters.Query.ToLowerInvariant()));
}
return await transactions.ToListAsync();
}
Controller:控制器:
[HttpGet]
public async Task<ActionResult<IEnumerable<TransactionsForUserVM>>>
GetTransactionsForSpecificUser5(
[FromQuery]QueryParameters queryParameters)
{
var email = HttpContext.User.RetrieveEmailFromPrincipal();
var list = await _transactionService.ShowTransactionsForSpecificUser2(queryParameters,
email);
return Ok(list);
}
Change this改变这个
.Where(t => t.Stock.ToLowerInvariant().Contains(queryParameters.Query.ToLowerInvariant()));
To this:对此:
.Where(t => t.Stock.Contains(queryParameters.Query));
EF Core doesn't know how to translate ToLowerInvariant into an SQL command, so it's throwing an error so you can choose to remove the operation or perform a client side evaluation (drag all the rows out the DB and then get c# to do the filter). EF Core 不知道如何将 ToLowerInvariant 转换为 SQL 命令,因此它会抛出错误,因此您可以选择删除操作或执行客户端评估(将所有行拖出数据库,然后让 c# 执行过滤器)。 Any time you see the "could not be translated" error it's a "you used some C# thing that has no supported mapping in SQL" indicator that you need to redesign some aspect of the C# (make it less complex)
每当您看到“无法翻译”错误时,它就是“您使用了一些在 SQL 中不支持映射的 C# 东西”指示符,您需要重新设计 C# 的某些方面(使其不那么复杂)
You can find a list of supported operations (what the C# is and what efcore can translate it to) here .您可以在此处找到支持的操作列表(C# 是什么以及 efcore 可以将其转换为什么)。 If you're not using SQLS, there are links to other DBs here .
如果你不使用SQLS,有链接到其他数据块在这里。 You might need to approach third party documentation sites if eg you're using something like postgres
如果例如您使用的是 postgres 之类的东西,您可能需要访问第三方文档站点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.