Entity Framework ToListAsync() with Select()

entity framework multiple async queries
entity framework group by async
linq select async lambda
entity framework remove async
the source iqueryable doesn't implement iasyncenumerable
queryableextensions
tolistasync vs tolist
toasyncenumerable

I have the following ActionResult in my controller

public async Task<ActionResult> NewTickets()
{
    // Show tickets for all divisions a agent is in

    var user = "abcdefg";
    var company = "company1";

    var tickets = (from a in db2.Ticket
        join c in db2.Division on a.DivisionId equals c.DivisionId
        join dp in db2.DivisionParticipator on c.DivisionId equals dp.DivisionId
        where c.CompanyId == company.CompanyId && a.Status == "New" && dp.ApplicationUserId == user.Id
        select new
        {
            Id = a.Id,
            DivisionId = a.DivisionId,
            Name = a.Name,
            TicketDate = a.TicketDate,
            NewPosts = a.NewPosts,
            Status = a.Status,
            Type = a.Type
         })
         .ToList().Select(x => new Ticket
         {
             Id = x.Id,
             DivisionId = x.DivisionId,
             Name = x.Name,
             TicketDate = x.TicketDate,
             NewPosts = x.NewPosts,
             Status = x.Status,
             Type = x.Type
          });            

    return PartialView(tickets.ToList());
}

What i want to achieve is running this async, however if adding the ToListAsync()before Select()the Select() method becomes unavailable. The same goes for the return PartialView(tickets.ToList()).

I'm fairly new to working with async but reading about it's advantages I have been convering regular ActionResult methods with async ones. This has been going fine so far with easier LINQ-based queries. However for this ActionResult-method the query is a bit more advanced and the syntax/way to implement it seems to differ.

Any help to send me in the right direction would be greatly appreciated!

Split in into two statements:

var tickets0 = await (from ...).ToListAsync();

var tickets = tickets0.Select(...);

The .ToListAsync() returns a Task, so it doesn't accept the .Select extension method, which will need some IEnumerable. Only when you await that Task, you will get the List.

Another way (less readable IMO) would be:

 var tickets = (await (from ...).ToListAsync()).Select(...);

Note the extra ( ) around the await clause, which mean that the Select will work on the result of the awaited code, instead of the Task.

QueryableExtensions.ToListAsync Method (System.Data.Entity , Entity Framework Core provides a set of async extension methods similar Examples include ToListAsync() , ToArrayAsync() , SingleAsync() . Entity Framework Core provides a set of async extension methods similar to the LINQ methods, which execute a query and return results. Examples include ToListAsync(), ToArrayAsync(), SingleAsync().

You will need to await the query before calling select, but given that the query is simply selecting a model out of the query you can create the model within the query and just await that.

public async Task<ActionResult> NewTickets()
{
    // Show tickets for all divisions a agent is in

    var user = "abcdefg";
    var company = "company1";

    var tickets = await (from a in db2.Ticket
        join c in db2.Division on a.DivisionId equals c.DivisionId
        join dp in db2.DivisionParticipator on c.DivisionId equals dp.DivisionId
        where c.CompanyId == company.CompanyId && a.Status == "New" && dp.ApplicationUserId == user.Id
        select new Ticket
        {
            Id = a.Id,
            DivisionId = a.DivisionId,
            Name = a.Name,
            TicketDate = a.TicketDate,
            NewPosts = a.NewPosts,
            Status = a.Status,
            Type = a.Type
         })
         .ToListAsync();

    return PartialView(tickets);
}

Asynchronous Queries, ToListAsync() method is made available by referencing the EntityFramework.dll and using System.Data.Entity; , it is only available on types that  It doesn't work, because you are mapping same fields again on same class, if you want to use EF generated class, you don't need to create DTO classes at all, just use context.Products.ToListAsync() and in the view .cshtml page you can use directly print data as Model.Name.

You also need to import System.Data.Entity rather than just System.Linq. That may seem silly, but I ended up on this question due to this. ToList is part of Linq but the async methods are specific to EF.

[SOLVED], IsDeleted) .Select(e => new PublisherWithMedia() { Id = e.Id, Name = e.Name, Mediae = e.Mediae.Select(m => ApiUtils.GetMedia(m)).ToList() };  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more ToListAsync() in a DbContext using statement : “The ObjectContext disposed”, How to deal?

[SOLVED], I am using Entity framework 6 and my query is as below: var query = await context.Products.Select(p => new Product() { Id = p.Id, Name = p. Entity Framework Knowledge Base; Online Examples; Return async task using LINQ select.net-core asp.net-core c# Your can use ToListAsync().

C#, The latest version of Entity Framework, Entity Framework 6, also has the Step 2​: Select the table to work with after defining the connection string. We called the ToListAsync() instead of calling ToList() method since we call  Adding Entity Data Model. In this section we'll add the ADO.NET Entity Data Model to the application. Step 1: Just right-click on the application and add the ADO.NET Entity Data Model. Step 2: Select the table to work with after defining the connection string. That's it. We have successfully added the data model in the application.

Working With Asynchronous Programming With Entity Framework, Select(phone => new Phone { Number = phone.Number }). ToList() }); var result = await query. EntityFrameworkCore; using Microsoft. But as you can see, you can easily use even async method from Entity Framework. If you customize the select also (the return type) you may need to either get the async result using reflection or use ExecuteAsync instead with ToList ().

Comments
  • ToListAsync will return a Task, not a List, so you can't Select over it - you will have to await ... .ToListAsync() and then select over that... I do wonder why you are ToListing at all at that point though.
  • Could you please explain why you need ToList in the middle of a query? Usualy ToList, ToArray called at the end of query to save enumeration result
  • This, together with what @Ant P suggested it's working flawlessly! Thank you!
  • This might work but it will bring back every column from the table query to memory as a result of the toList and then do the projection there rather than only bringing back the projected columns.