Search code examples
c#entity-frameworkentity-framework-5

EF5 IQueryable does not contain a definition for ToListAsync


I have this code, and i dont figured out what is wrong with my code. I have EF 5 and i want to make a Task await method.

What i want to do is to select query a simple table but in async way. but the method ToListAsync() it seems is not in my EF.

How can i solve this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EO;
using System.Data.Objects;
using System.Data.Objects.SqlClient;
using System.Transactions;
using System.Threading;
using System.Data.Entity;

namespace DimexCEDatos
{
    static public class AfiliadosDatos
    {
        static List<myclass> lstSegmentos = new List<myclass>();

        public static async Task<List<myclass>> myfunction()
        {
            try
            {
                using (MyDB db = new MyDB())
                {
                    lstSegmentos = await (from item in db.genAfiliados
                                          select item).AsQueryable().ToListAsync();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return lstSegmentos;
        }
    }
}

Solution

  • I'm not totally sure but maybe you can use extension method like below

    public static class SampleExtension
    {
        public static async Task<List<T>> ToListAsync<T> (this IQueryable<T> obj) => await Task.Run(() => obj.ToList());
    }