Entity Framework 4 Unit of Work / Repository WITHOUT .Query() Lambda
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.
精彩评论