user1288943
user1288943

Reputation: 161

.Error CS1061 'Task<IEnumerable>' does not contain a definition for 'Where' and no accessible extension method 'Where'

My generic repository is as follows ( I have similar in synchronious and it works)

public virtual async Task<IEnumerable<TEntity>> GetAsyn(
    Expression<Func<TEntity, bool>> filter = null,
    Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
    string includeProperties = "")
{
    IQueryable<TEntity> query = dbSet;

    if (filter != null)
    {
        query = query.Where(filter);
    }

    foreach (var includeProperty in includeProperties.Split
        (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
    {
        query = query.Include(includeProperty);
    }

    if (orderBy != null)
    {
        return await orderBy(query).ToListAsync();
    }
    else
    {
        return await query.ToListAsync();
    }
}

when I call this:

var custDB = await unitOfWork.CustomerRepository
                             .GetAsyn()
                             .Where(c => c.UserId == userID && c.IsDeleted != true)
                             .FirstOrDefault();

I get the following error...Can someone help me fix this

...Error CS1061 'Task<IEnumerable>' does not contain a definition for 'Where' and no accessible extension method 'Where' accepting a first argument of type 'Task<IEnumerable>' could be found (are you missing a using directive or an assembly reference?) ..

Upvotes: 0

Views: 9093

Answers (4)

theemee
theemee

Reputation: 834

GetAsyn returns a task, you should await it first.

But anyway why would you do the filtering on the client? Your GetAsyn method has a filter parameter, so you should write it like this:

var custDbList = await unitOfWork.CustomerRepository
                                 .GetAsyn(c => c.UserId == userID && c.IsDeleted != true)
var custDB = custDbList.FirstOrDefault();

Upvotes: 1

Ali
Ali

Reputation: 1210

am not sure if this is still valid or needed ,but here is my input

you just need the ToListAsync() method, which is in the System.Linq.Async package .

some code to help

using part :

using System;
sing System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

code :

public async Task<JsonResult> getgaAreasList(string silter)
{
    return Json(await new gaAreasVM()
                          .GetAll()
                          .Where(x=>x.name == filter)  
                          .ToListAsync());
}

Upvotes: 0

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89361

You could make this work by calling

var customers = await unitOfWork.CustomerRepository.GetAsyn();
var customer = customers.Where(c => c.UserId == userID && c.IsDeleted != true).FirstOrDefault();

But that would perform the filtering on the client. And your "generic repository" doesn't really do anything useful, so you should just delete that code and run:

var customer = await db.Customers.Where(c => c.UserId == userID && c.IsDeleted != true).FirstOrDefaultAsync();

Upvotes: 1

Genusatplay
Genusatplay

Reputation: 771

Because GetAsyn() return Task. You need await result of task.

var result = await unitOfWork.CustomerRepository.GetAsyn()
var custDB = result.Where(c => c.UserId == userID && c.IsDeleted != true)
                   .FirstOrDefault();

Upvotes: 1

Related Questions