score:18
async lambda expression cannot be converted to simple func<tsource, tresult>
.
so, select many cannot be used. you can run in synchronized context:
myenumerable.select(c => functions.getdataasync(c.id)).selectmany(task => task.result);
or
list<dataitem> result = new list<dataitem>();
foreach (var ele in myenumerable)
{
result.addrange(await functions.getdataasyncdo(ele.id));
}
you cannot neither use yield return
- it is by design. f.e.:
public async task<ienuemrable<dataitem>> do()
{
...
foreach (var ele in await functions.getdataasyncdo(ele.id))
{
yield return ele; // compile time error, async method
// cannot be used with yield return
}
}
score:1
with c#8 and iasyncenumerable
we can write this more naturally:
public static async iasyncenumerable<r>
selectmanyasync<t, r>(this ienumerable<t> ts, func<t, task<ienumerable<r>>> func)
{
foreach (var t in ts)
{
var rs = await func(t);
foreach (var r in rs)
yield return r;
}
}
note:
use async foreach(...
to iterate over an iasyncenumerable
score:8
select
works because it will return an ienumerable<task<t>>
, which can then be awaited with e.g. task.whenall
.
so, an easy workaround to this problem is:
ienumerable<task<ienumerable<t>>> tasks = source.select(getnestedenumerabletask);
ienumerable<t>[] nestedresults = await task.whenall(tasks);
ienumerable<t> results = nestedresults.selectmany(nr => nr);
score:58
this is an extension:
public static async task<ienumerable<t1>> selectmanyasync<t, t1>(this ienumerable<t> enumeration, func<t, task<ienumerable<t1>>> func)
{
return (await task.whenall(enumeration.select(func))).selectmany(s => s);
}
that allows you to run:
var result = await myenumerable.selectmanyasync(c => functions.getdataasync(c.id));
explanation: you have a list of tasks, each returns task<ienumerable<t>>
. so you need to fire them all, then await all, and then squash the result via selectmany.
Source: stackoverflow.com
Related Query
- How to use async lambda with SelectMany?
- How can I use "Where" with an async predicate?
- How to use async within a lambda which returns a collection
- How to: Use async methods with LINQ custom extension method
- How to use an expression with a generic func lambda in a linq where clause?
- how to use a method with 2 parameters in my lambda expression
- ASP.NET MVC: How to use linq lambda expression with a ViewModel to get data?
- How to use a method with an out parameter in a lambda expression
- how to use lambda with leftjoin and isnull check
- How to use Linq or Lambda with IQueryable to GroupBy and get the first/last record on the collection in C#?
- How to use selectmany with condition in c#
- How to use method-based queries with lambda expressions
- How to use .Where with lambda / IQueryable
- How to use sql IN statement in linq lambda with 2 statements
- How to use .Top() with Code First
- How to use Inner Join with Lambda expression?
- How to use lambda expression with joins
- How to use pagelist with data source as multiple tables, to display table values in mvc?
- How to use LINQ to select object with minimum or maximum property value
- How to use orderby with 2 fields in linq?
- How to use LINQ Distinct() with multiple fields
- How to use LINQ with dynamic collections
- How to use Lambda in LINQ select statement
- What does this C# code with an "arrow" mean and how is it called?
- Can I use Linq's Except() with a lambda expression comparer?
- how to use entity framework to group by date not date with time
- How do you use LINQ with Sqlite
- How do I use the AsQueryable method asynchronously with MongoDb C# Driver 2.1?
- How Build Lambda Expression Tree with multiple conditions
- How to use index/position with Where in LINQ query language?
More Query from same tag
- Is this LINQ dynamic orderby method thread safe?
- ef-core load collection property of nested tph inherited member
- Unexpected behavior of the `Where` clause
- How do I combine these similar linq queries into one?
- How to write OrderByDescending and where clause in single LinQ query
- C# How do I replace objects in one collection with Objects from another collection
- Dynamic Order By Entity Framework
- C# LINQ Include
- linq to sql LoadWith limiting fields returned
- C#: Update Item value in List<of T>
- Remove duplicates and get only records with the earliest date in a LINQ expression
- Dynamically setting LINQ datasource results in Operator '==' incompatible with operand types 'String' and 'Int32'
- How is using Entity + LINQ not just essentially hard coding my queries?
- Dynamic LINQ filter never retrieves correct results (UWP/XAML)
- Linq to sql, query many-to-many with count of other
- Linq return JSON
- How to get PEX to automatically generate inputs for code involving LINQ
- Is is possible to add .Where() on a child collection property using nhibernate linq?
- Catch the error from Stored Procedure in C#
- .NET LINQ to entities group by date (day)
- Calculate Running Total in Linq
- Building up an object in linq - adding properties that depend on other properties
- StreamWriter all lines to file
- How to use LINQ to order within groups
- retrieve the names depends upon the price conditions
- LINQ vs nested loop
- Visual studio is locking up after loading entity framework database connection
- C# LINQ Distinct(f=>f.propertyname) doesnt work
- Average int Array elements with a GroupBy
- Order collection of items with multiple date fields