开发者

Entity Framework 4 Unit of Work / Repository WITHOUT .Query() Lambda

开发者 https://www.devze.com 2023-03-14 18:53 出处:网络
We\'ve seen some very useful methods of implementing EF4 Repository and Unit of Work pattern (Reference 1, Reference 2)

We've seen some very useful methods of implementing EF4 Repository and Unit of Work pattern (Reference 1, Reference 2)

All the examples I see use methods requiring the use of LINQ Expression Syntax, e.g.:

IEnumerable<T> Query(Expression<Func<T, bool>> filter) 

var employees = employeeRepository.Query(emp => emp.Username == userName); 

But I want to use LINQ Query Expression syntax, e.g.:

from emp in context.Employees select emp where emp.Username == userName;

Is there a way to enjoy the benefits of a repository and UoW wh开发者_如何学Goilst being able to use LINQ Query Expression syntax in my repository methods?

Richard


Yes but you must expose IQueryable<T>:

IQueryable<T> Query();

After that you can use code like:

var query = from emp in EmployeeRepository.Query() 
            where emp.Username == userName
            select emp;

The difference between approaches was discussed yesterday. Also whole concept of repository and unit of work was discussed many times - including why to use them and what problems you can expect.

0

精彩评论

暂无评论...
验证码 换一张
取 消