C# Entity Framework Base Repository

Posted by Andy on Stack Overflow See other posts from Stack Overflow or by Andy
Published on 2010-05-01T17:42:13Z Indexed on 2010/05/01 17:47 UTC
Read the original article Hit count: 257

Filed under:
|

I'm trying to create a base repository for use with Entity Framework 4.0 and having some trouble. In this code below, why is it not possible to do this in one line?

    public IEnumerable<T> GetAll<T>(Expression<Func<T, bool>> filter)
    {
        IEnumerable<T> allCustomers = this.GetAll<T>();
        IEnumerable<T> result = allCustomers.Where(filter.Compile());

        return result;
    }

Won't this result in 2 SQL statements: one without a where clause that retrieves all rows, and one with a where clause that only retrieves the rows that match the predicate?

How can this be done with a single SQL statement? I can't get it to compile if I try to cast the filter.Compile() to Func<Customer, bool>.

Thanks,

Andy

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about LINQ