Integrate cache in the query.

One of my customers wanted to do some LINQ To Entities queries with a where.

For example, he wants to do context.EmployeeSet.Where (e => e.Manager == null).

Ok. Now he wants to include the context additions and to exclude the context deletions in his result. 

I do this:

public static class QueryableExtension

{

    public static IEnumerable<T> IntegrateContext<T>(this IQueryable<T> source, ObjectContext context) where T : EntityObject

    {

        return IntegrateContext(source, context, t => true);

    }

    public static IEnumerable<T> IntegrateContext<T>(this IQueryable<T> source, ObjectContext context, Func<T, bool> where) where T : EntityObject

    {

        return source.AsEnumerable().Where(e => e.EntityState != EntityState.Deleted).Union(context.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Select(ose => ose.Entity).OfType<T>()).Where(where);

    }

}


Now, the idea is to define only once the where.

For this, I can do this:

 

Expression<Func<Employee, bool>> where = e => e.Manager == null;

var q = context.EmployeeSet.Where(where).IntegrateContext(context, where.Compile());



 


 


 


This entry was posted in 7671, 7674. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>