Home > Back-end >  DbSet<> does not contain definition for GetAwaiter
DbSet<> does not contain definition for GetAwaiter

Time:10-31

I have this search method that gives me some headache

var query = await dbContext.Tasks; // Gets the error here!

How can I fix this?

Here is my code:

        public static async Task<IEnumerable<BKR.BOL.Task>> Search(string Name, string Description)
        {
            MyDbContext dbContext = new BKR.DAL.Context.MyDbContext();
            var query = await dbContext.Tasks; // Gets the error here!
            if (!string.IsNullOrEmpty(Name))
            {
                query = query.Where(x => x.Name == Name).ToList();
            }
            if (!string.IsNullOrEmpty(Description))
            {
                query = query.Where(x => x.Description == Description).ToList();
            }

            return MapToBOL(query, dbContext);
        }
public static List<BKR.BOL.Task> MapToBOL(IList<DAL.Task> data, MyDbContext db)
        {
            var query = from d in data

                        select new Task(d, db)
                        {
                            TaskId = d.TaskId,
                            TaskTypeId = d.TaskTypeId,
                            TaskStatusId = d.TaskStatusId,
                            CustomerId = d.CustomerId,
                            ResourceId = d.ResourceId,
                            Deleted = d.Deleted,
                            Name = d.Name,
                            Description = d.Description,
                            StartTime = d.StartTime,
                            EndTime = d.EndTime,
                            CreatedBy = d.CreatedBy,
                            CreationTime = d.CreationTime,
                            ChangedBy = d.ChangedBy,
                            ChangedTime = d.ChangedTime
                        };
            return query.ToList();
        }

CodePudding user response:

dbContext.Tasks is a collection of task objects, not a query, and is not awaitable.

If you want the data, you'll need to create a query, such as...

var query = await dbContext.Tasks.ToListAsync();

Note that ToListAsync returns a Task (the built-in one, not your database type), and so is awaitable.

If you want to handle multiple Where clauses, you can do it like this...

var query = dbContext.Tasks;
if (something) {
  query = query.Where(t => t.Id == "Ferret")
}
// Other clauses can go here
var results = await query.ToListAsync();

CodePudding user response:

I have reached to this solution.....what you think about that?

public static async Task<List<BKR.BOL.Task>> Search(bool Deleted, string Name, string Description, System.DateTime? StartTime, System.DateTime? EndTime, System.DateTime CreationTime, System.DateTime? ChangedTime)
        {
            MyDbContext dbContext = new BKR.DAL.Context.MyDbContext();
            var query = dbContext.Tasks.AsQueryable();
            if (!string.IsNullOrEmpty(Name))
            {
                query = query.Where(x => x.Name == Name);
            }
            if (!string.IsNullOrEmpty(Description))
            {
                query = query.Where(x => x.Description == Description);
            }

            return await MapToBOLAsync(query, dbContext);
        }


public static Task<List<BKR.BOL.Task>> MapToBOLAsync(IQueryable<DAL.Task> data, MyDbContext db)
        {
            var query = from d in data
                        select new Task(d, db)
                        {
                            TaskId = d.TaskId,
                            TaskTypeId = d.TaskTypeId,
                            TaskStatusId = d.TaskStatusId,
                            CustomerId = d.CustomerId,
                            ResourceId = d.ResourceId,
                            Deleted = d.Deleted,
                            Name = d.Name,
                            Description = d.Description,
                            StartTime = d.StartTime,
                            EndTime = d.EndTime,
                            CreatedBy = d.CreatedBy,
                            CreationTime = d.CreationTime,
                            ChangedBy = d.ChangedBy,
                            ChangedTime = d.ChangedTime
                        };
            return query.ToListAsync();
        }
  • Related