User1400794712 posted
Hi tvb2727,
If you want to call GetChildren() asynchronously, you can use async-await for it.
async Task<List<Foo>> GetChildren(List<Foo> foos, int id)
{
var re = await foos.AsQueryable()
.Where(x => x.ParentId == id)
.Union(foos.Where(x => x.ParentId == id)
.SelectMany(y => GetChildren(foos, y.Id).Result)
).ToListAsync() ;
return re;
}
If you want to call this method in MVC controller, we should also add async for the action:
public async Task<ActionResult> LinqTask()
{
List<Foo> foos = new List<Foo>();
foos.Add(new Foo { Id = 1 });
foos.Add(new Foo { Id = 2, ParentId = 1 });
foos.Add(new Foo { Id = 3, ParentId = 2 });
foos.Add(new Foo { Id = 4 });
var list = await GetChildren(foos, 1);
return View();
}
Best Regards,
Daisy