.Net Entity Framework & POCO ... querying full table problem
I'm attempting to implement a repository pattern with my poco objects auto generated from my edmx.
In my repository class, I have:
IObjectSet<E> _objectSet;
private IObjectSet<E> objectSet
{
get
{
if (_objectSet == null)
{
_objectSet = this._context.CreateObjectSet<E>();
}
return _objectSet;
}
}
public IQueryable<E> GetQuery(Func<E, bool> where)
{
return objectSet.Where(where).AsQueryable<E>();
}
public IList<E> SelectAll(Func<E, bool> where)
{
return GetQuery(where).ToList();
}
Where E is the one of my POCO classes. When I trace the database and run this:
IList<Contact> c = contactRepository.SelectAll(r => r.emailAddress == "myemail@email.com");
It shows up in the sql trace as a select for everything in my Contact table. Where am I going wrong here? Is there a better way to do this? Does an objectset not lazy load... so it omitte开发者_Python百科d the where clause? This is the article I read which said to use objectSet's... since with POCO, I do not have EntityObject's to pass into "E"
http://devtalk.dk/CommentView,guid,b5d9cad2-e155-423b-b66f-7ec287c5cb06.aspx
Use Expression<Func<E, bool>>
instead of Func<E, bool>
. The first one tells the C# compiler to emit an expression tree (used to build the SQL query) instead of actual code, where as the second is a normal delegate. That means you're currently doing the filtering with Linq to Objects after the database call.
精彩评论