'.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?) ..
Solution 1:[1]
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();
Solution 2:[2]
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();
Solution 3:[3]
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());
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Genusatplay |
Solution 2 | David Browne - Microsoft |
Solution 3 |