Here is my source code
public object Index2()
{
List<Product> products;
using (var dbContext = new MarketContext())
{
products = dbContext.Products.Select(x => x).ToList();
}
return products;
}
public object Index3()
{
List<Category> categories;
using (var dbContext = new MarketContext())
{
categories = dbContext.Categories.Select(x => x).ToList();
}
return categories;
}
And i want to make a method to reuse "using dbContext"
but i have no idea how to do this
here is my trying code
public object Index3()
{
var sql = ****.Category.Select(x => x);
var categories = Query<Category>(sql);
return categories;
}
public static List<T> Query<T>(IQueryable<T> sql)
{
using (var dbContext = new MarketContext())
{
var result = ******sql.ToList();
return result;
}
}
I really want know how to achieve this and is it possible?
Thanks for help