0

I have a database and I make a lot of Where calls to it to make a table in Blazor. This takes long because Where is not Async. To speed this up I wanted to change the Where statements by using: 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' ...

How do I resolve this issue?

Original post where code is based on: How can I use "Where" with an async predicate? The last answer.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • Why do you need that? And it is wrong that it is slow because `Where is not Async`. It is slow because you put `ToAsyncEnumerable` on top of the table. You have to apply `Where` first. Then `ToListAsync()` – Svyatoslav Danyliv Jan 26 '22 at 08:54
  • Actually `async` operations are slower than synchronous versions. But in couple when APP Server is under high load, `async` relaxes server's threads usage. – Svyatoslav Danyliv Jan 26 '22 at 08:58
  • For 1 table I have to make +- 100 database calls. This Database is not on the same server, therefore I wanted to make these calls Asyncronous. Would that not make it a lot faster? – Harold Vandeputte Jan 26 '22 at 09:00

1 Answers1

0

Where is operator for filtering data and it is DO NOT EXECUTES query, it just defines filter for query. So no asyncs is needed for Where. You have to apply it on IQueryable to filter data.

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.
Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32