Это работаетIQueryable метод расширения без прохождения DbContext в качестве параметра
var invoices = this.myContext.FilterByCustomer(this.myContext.Invoices, customerId);
Он реализован в виде:
public partial class MyContext : DbContext
{
public IQueryable<T> FilterByCustomer<T>(IQueryable<T> queryableEntityCollection, int customerId) where T : class, ICustomerEntity
{
// I need to query entities from MyContext here
// This implementation already works
}
}
Но я хочу это
var invoices = this.myContext.Invoices.FilterByCustomer(customerId);
Если я реализую расширение метод на IQueryable (DbSet), кажется, что я должен передать MyContext
в качестве параметра, который я не как.
public static IQueryable<T> FilterByCustomer<T>(this IQueryable<T> queryableEntityCollection, MyContext context, int customerId) where T : class, ICustomerEntity
{
// I need to query entities from MyContext here
// This WOULD work, I would be able to query other tables on 'context', but I don't like passing the context as parameter here
// I don't want this implementation
}
Как я могу реализовать IQueryable расширение, которое не требует меня передать контекст в качестве параметра?
public IQueryable<T> FilterByCustomer<T>(IQueryable<T> queryableEntityCollection, int customerId) where T : class, ICustomerEntity
{
// I need to query entities from MyContext here, without passing MyContext as a parameter
// I want such implementation
}
Возможно ли это вообще?
Это потрясающе, спасибо –