简体   繁体   English

如何使用 System.Linq.Async 中的 WhereAwait 和自定义布尔值

[英]How to use WhereAwait from System.Linq.Async with custom bools

I have a database and I make a lot of Where calls to it to make a table in Blazor.我有一个数据库,我对它进行了很多Where调用以在 Blazor 中创建一个表。 This takes long because Where is not Async.这需要很长时间,因为Where不是异步的。 To speed this up I wanted to change the Where statements by using: ToAsyncEnumerable().WhereAwait from System.Linq.Async.为了加快速度,我想使用以下方法更改Where语句: ToAsyncEnumerable().WhereAwait from System.Linq.Async。

This is my line of code:这是我的代码行:

series5 = _context.ChickenSeries.ToAsyncEnumerable().WhereAwait(async serie => await ((serie.DatumWeek5 >= firstDay && serie.DatumWeek5 <= lastDay) && serie.SlaughterHouse.SlaughterHouseId != LeegstandID));

The error presenting on this line: 'bool' does not contain a definition for 'GetAwaiter'...此行出现的错误: “bool”不包含“GetAwaiter”的定义...

How do I resolve this issue?我该如何解决这个问题?

Original post where code is based on: How can I use "Where" with an async predicate?代码基于的原始帖子: 如何将“Where”与异步谓词一起使用? The last answer.最后一个答案。

Where is operator for filtering data and it is DO NOT EXECUTES query, it just defines filter for query.过滤数据的运算符Where ,它是不执行查询,它只是定义查询的过滤器。 So no asyncs is needed for Where .所以asyncs不需要异步Where You have to apply it on IQueryable to filter data.您必须将其应用于IQueryable以过滤数据。

Try to rewrite your query in the following way:尝试通过以下方式重写您的查询:

var result = await _context.ChickenSeries
    .Where(serie => (serie.DatumWeek5 >= firstDay && serie.DatumWeek5 <= lastDay) && serie.SlaughterHouse.SlaughterHouseId != LeegstandID)
    .ToListAsync();

If it is still slow, there are options:如果它仍然很慢,有以下选择:

  1. Number of returned records is too high返回的记录数过多
  2. Database has no proper indexes and you have to analyze SQL Server execution plan.数据库没有适当的索引,您必须分析 SQL 服务器执行计划。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM