如何针对集合调用 Expression<Func<Entity, bool>>
How to invoke Expression<Func<Entity, bool>> against a collection
我有一个从存储库模式定义存储库的接口:
interface IRepository
{
List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression);
}
我已经针对 Entity Framework 实施了它:
class EntityFrameworkRepository
{
public List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression)
{
return DBContext.Customers.Where(expression).ToList();
}
}
这似乎工作得很好,它允许我做类似的事情:
var customers = entityFrameworkRepository.Where(
customer => String.IsNullOrEmpty(customer.PhoneNumber)
);
现在我想要一个用于测试和演示目的的 InMemoryRepository。我试图创建一个:
class InMemoryRepository
{
Dictionary<int, Customer> Customers {get; set;} = new Dictionary<int, Customer>();
public List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression)
{
//what do I put here?
}
}
正如您在上面的代码中看到的,我对如何实现 InMemoryRepository.GetAllCustomers
感到困惑。我应该怎么做才能通过提供的表达式和 return 结果过滤客户?
我试过了:
return Customers.Where(expression));
但显然它期待 Func<KeyValuePair<int, Customer>, bool>
所以我得到一个编译错误:
Error CS1929 'Dictionary' does not contain a definition for 'Where' and the best extension method overload 'Queryable.Where(IQueryable, Expression>)' requires a receiver of type 'IQueryable' DataAccess.InMemory
尝试.AsQueryable()方法:
return Customers.Values.AsQueryable().Where(expression);
例子
Expression<Func<Products, bool>> expresionFinal = p => p.Active;
if (mydate.HasValue)
{
Expression<Func<Products, bool>> expresionDate = p => (EntityFunctions.TruncateTime(c.CreatedDate) <= mydate);
expresionFinal = PredicateBuilder.And(expresionFinal, expresionDate );
}
IQueryable<T> query = dbSet;
query = query.Where(expresionFinal);
我有一个从存储库模式定义存储库的接口:
interface IRepository
{
List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression);
}
我已经针对 Entity Framework 实施了它:
class EntityFrameworkRepository
{
public List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression)
{
return DBContext.Customers.Where(expression).ToList();
}
}
这似乎工作得很好,它允许我做类似的事情:
var customers = entityFrameworkRepository.Where(
customer => String.IsNullOrEmpty(customer.PhoneNumber)
);
现在我想要一个用于测试和演示目的的 InMemoryRepository。我试图创建一个:
class InMemoryRepository
{
Dictionary<int, Customer> Customers {get; set;} = new Dictionary<int, Customer>();
public List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression)
{
//what do I put here?
}
}
正如您在上面的代码中看到的,我对如何实现 InMemoryRepository.GetAllCustomers
感到困惑。我应该怎么做才能通过提供的表达式和 return 结果过滤客户?
我试过了:
return Customers.Where(expression));
但显然它期待 Func<KeyValuePair<int, Customer>, bool>
所以我得到一个编译错误:
Error CS1929 'Dictionary' does not contain a definition for 'Where' and the best extension method overload 'Queryable.Where(IQueryable, Expression>)' requires a receiver of type 'IQueryable' DataAccess.InMemory
尝试.AsQueryable()方法:
return Customers.Values.AsQueryable().Where(expression);
例子
Expression<Func<Products, bool>> expresionFinal = p => p.Active;
if (mydate.HasValue)
{
Expression<Func<Products, bool>> expresionDate = p => (EntityFunctions.TruncateTime(c.CreatedDate) <= mydate);
expresionFinal = PredicateBuilder.And(expresionFinal, expresionDate );
}
IQueryable<T> query = dbSet;
query = query.Where(expresionFinal);