I have a List
of elements which is populated through an async
call of a WebService (no problem there).
I need to filter that list in order to show something on the application view. I tried this:
List<DateTime> dates = EventsDates.Where(x => x.Day == tmp.Day && x.Month == tmp.Month && x.Year == tmp.Year).ToList();
I also tried using the Select
function of LINQ. The problem is that any time this code is called, the EventsDates
list isn't populated, the WebService hasn't responded yet.
How could I try to filter the EventsDates
list asynchronously?
"Where" shouldn't be async, you need to do EventsDates fill awaitable. Something like this:
EventsDates = await YourWebServiceMethodCall();
List<DateTime> dates = EventsDates.Where (x => x.Day == tmp.Day && x.Month == tmp.Month && x.Year == tmp.Year).ToList();
You can call it asynchronously like this but as others have stated you need to await the eventDates to unwrap the response from the service before you use it. @Dmitry's answer is correct. I just put this here to answer your question of how to use Where asynchronously in case you need it in the future.
using System.Data.Entity;
List<DateTime> dates = await EventsDates.Where(x => x.Day == tmp.Day && x.Month == tmp.Month && x.Year == tmp.Year).ToListAsync();
You can also do this:
var list = someCollection.Where(async x => x.Prop == await SomeLongRunningProcess());
It looks like you aren't await
ing the result to populate EventDates
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With