Hi,
in my never ending quest to cut down on all the code repetition that (I feel) LINQ causes I am trying to write a method that will add the 'standard' prefetches for my entity.
An example: I have Project, referencing Project is Activity. Now for my various Project queries that prefetch Activity I find myself rewriting the same prefetches over and over, (the Prefetch, the OrderBy, the FilterOn etc). I want this logic defined in a single location that various queries can reuse whenever they prefetch Activity.
To accomodate this I came up with this method:
internal static IPathEdgeParser<T, ActivityEntity> PrefetchCore<T>(ref IQueryable<T> query, Expression<Func<T, object>> expression) where T:EntityBase2
{
IPathEdgeParser<T, ActivityEntity> prefetch = null;
query = query.WithPath(p => (prefetch = p.Prefetch<ActivityEntity>(expression).
OrderBy(a => a.DueDate).OrderBy(a => a.Name)).
SubPath(a => a.Prefetch<UserEntity>(t => t.UserUsingAssignedTo)));
if (!Thread.CurrentPrincipal.IsInRole("Can_Access_Internal_Activity") && prefetch != null)
prefetch.FilterOn(p => p.IsInternal == false);
return prefetch;
}
Calls look like:
Activity.PrefetchCore(ref project, p => p.ActivityUsingProjectId);
"project" is an IQueryable<ProjectEntity>.
This all works. However it blows away any prefetches I may have already added to the IQueryable, also if I try to add more after the call it blows away what was added in my PrefetchCore method call.
I understand that WithPath replaces what was created before. So is it possible to get a reference to the existing prefetch and add to it??