I want to create generic method to get few columns only. I am using entity framework with repository pattern.
I tried with below
public async Task<IEnumerable<TResult>> GetSelectedColumns<TResult>(Func<T, TResult> selector,
Expression<Func<T, bool>> predicate = null)
{
IQueryable<T> query = dbSet;
if (predicate != null)
{
query = query.Where(predicate);
}
return await query.Select(selector).ToListAsync();
}
I am getting compile time error as below
Error CS1061 'IEnumerable<TResult>' does not contain a definition for 'ToListAsync' and no accessible extension method 'ToListAsync' accepting a first argument of type 'IEnumerable<TResult>' could be found (are you missing a using directive or an assembly reference?)
Please note; I have already included reference for Microsoft.EntityFramework
What else I am missing? is this correct way to get few columns using entity framework?
Change type of the selector
parameter to Expression<Func<T, TResult>>
. But why not simply expose the DbSet<TEntity>
as an IQueryable<TEntity>
?