我有一个从存储库模式定义存储库的接口:
interface IRepository
{
List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression);
}
我已经针对实体框架实现了它:
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
执行。我应该做什么来按提供的表达式过滤客户并返回结果?
I tried:
return Customers.Where(expression));
但显然它期待着Func<KeyValuePair<int, Customer>, bool>
所以我得到一个编译错误:
错误 CS1929“字典”不包含“Where”的定义,并且最佳扩展方法重载“Queryable.Where(IQueryable, Expression>)”需要“IQueryable”DataAccess.InMemory 类型的接收器