1

Any ways to shorten this include statement?

var query = Context.Businesses .Include(b => b.Categories) .Include(b => b.Branches.Select(br => br.Address)) .Include(b => b.Branches.Select(br => br.BranchType)) .Include(b => b.Branches.Select(br => br.CustomFields)) .Include(b => b.Branches.Select(br => br.Phones)) .Include(b => b.Branches.Select(br => br.OpeningTimes.Select(ot => ot.WorkingPeriods))); 

I thought about using a SPROC but I'm unsure how it will know what was returned.

So is there a non hard-coded way to do this shorter than it is? Perhaps an external lambda that treats all the properties of Branch?

1 Answer 1

1

You could do something similar to the answer to this question. So in your case, make an extension method like this:

public static class DataContextExtensions { public static IQueryable<Business> BusinessesComplete(this DataContext context){ return context.Businesses .Include(b => b.Categories) .Include(b => b.Branches.Select(br => br.Address)) .Include(b => b.Branches.Select(br => br.BranchType)) .Include(b => b.Branches.Select(br => br.CustomFields)) .Include(b => b.Branches.Select(br => br.Phones)) .Include(b => b.Branches.Select(br => br.OpeningTimes.Select(ot => ot.WorkingPeriods))); } } 

Then use it like this:

Business business = context.BusinessesComplete().Where(b => ...etc); 
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to do compile this function so the generated SQL doesn't have to be calculated each time again?
In EF 5 this is availeble for LINQ queries and enabled by default, not sure if you can enable it in earlier versions though. Worth reading this: msdn.microsoft.com/en-us/data/hh949853.aspx

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.