The following code example works of course:
var ints = new List<int> { 1, 2, 3 };
var smallInts = ints.Where(i => i < 3);
But what if need to call an async method in a Where condition?
var ints = new List<int> { 1, 2, 3 };
var smallInts = ints.Where(async i => { return await IsSmallInt(i); });
This results in the following error:
Error 21: Cannot convert async lambda expression to delegate type 'System.Func'. An async lambda expression may return void, Task or Task, none of which are convertible to 'System.Func'.
Of course I could just write a foreach loop in this case, but I don't want to.
var smallInts = new List<int>();
foreach (int i in ints)
if (await IsSmallInt(i))
smallInts.Add(i);
So what's the best way (and maybe also the shortest way) to use await in a Where condition?