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());