我没有Expression<Func<DomainEntities.Customer, bool>>在那篇文章中,但是我总是在IRepository<T>界面中找到一个Find方法.界面:IEnumerable<T> Find(Expression<Func<T, bool>> expression, int maxHits = 100);以及抽象基础存储库中的实现:public virtual IEnumerable<T> Find(Expression<Func<T, bool>> expression, int maxHits = 100) { return this.DataContext.DbSet<T>().Where(expression).Take(maxHits);}现在您可以通过lambda表达式在任何实体上调用Find ...如果您做得不好,我可以发表一个完整的例子,只要说出什么时候即可.I've been reading Tim McCarthy's awesome book on DDD in .NET. In his example application though, his underlying data access is using SqlCE and he's handcrafting the SQL inline.I've been playing with some patterns for leveraging Entity Framework but I've gotten stuck on how exactly to map the IRepository linq queries to the underlying data access layer.I have a concrete repository implementation called.public EFCustomerRepository : IRepository<DomainEntities.Customer>{ IEnumerable<DomainEntities.Customer> GetAll( Expression<Func<DomainEntities.Customer, bool>> predicate) { //Code to access the EF Datacontext goes here... }}In my EF Model, I'm using POCO Entities but even so there's going to be no native mapping between my DomainEntity.Customer & my DataAccessLayer.Customer objects.so I can't just pass Expression<Func<DomainEntities.Customer, bool>> predicate as the parameter for an EFContext.Customers.Where(...);Is there an easy way to map anExpression<Func<T, bool>> predicate => Expression<Func<TOTHER, bool>> predicateOr am I going about this all wrong ?Any suggestions / pointers appreciated. 解决方案 From the code provided in your example I guess you are not using a generic repository pattern?I use EF CodeFirst (but it works for old EF to) with a generic repository pattern... http://average-uffe.blogspot.com/2011/03/repository-pattern-with-ef-code-first.htmlI do not have the Expression<Func<DomainEntities.Customer, bool>>in that post, but I always have a Find metod in the IRepository<T> interface.The interface:IEnumerable<T> Find(Expression<Func<T, bool>> expression, int maxHits = 100);And the implementation in the abstract baserepository:public virtual IEnumerable<T> Find(Expression<Func<T, bool>> expression, int maxHits = 100) { return this.DataContext.DbSet<T>().Where(expression).Take(maxHits);}And now you can call Find on any entity by a lambda expression...I can post a full example if you do not get it right, just say when. 这篇关于在模型和模型之间是否有使用LINQ的建议模式?基于DDD的分层体系结构中的DataAccess层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!