Entity Framework - Programatically add Includes to ObjectQuery?

Well, since Include returns an IQueryable WithIncludes(this IQueryable source, string associations) { var query = (ObjectQuery)source; foreach (var assoc in associations) { query = query. Include(assoc); } } Then your query looks like this: var inclusions = new { "Tests. Course", "Tests.

TestEvents" }; var result = ctx. AppUsers . WithIncludes(inclusions) .

Where(x => x.Login.ToLower() == loginName.ToLower()) . Where(x =>!x. IsDeleted) .FirstOrDefault() To get some strong-typing into the otherwise magic-string nature of include, I have specialized enums for all associations, so I pass an of those through and convert the enum to the string include The thing is, your methods should define what inclusions are required.So first thing you do in the method is declare what associations are required, then pass that to your extension method Is that what your after?

Well, since Include returns an IQueryable (LINQ chainable), I would use a simple extension method: public static IQueryable WithIncludes(this IQueryable source, string associations) { var query = (ObjectQuery)source; foreach (var assoc in associations) { query = query. Include(assoc); } } Then your query looks like this: var inclusions = new { "Tests. Course", "Tests.

TestEvents" }; var result = ctx. AppUsers . WithIncludes(inclusions) .

Where(x => x.Login.ToLower() == loginName.ToLower()) . Where(x =>!x. IsDeleted) .FirstOrDefault(); To get some strong-typing into the otherwise magic-string nature of include, I have specialized enums for all associations, so I pass an of those through and convert the enum to the string include.

The thing is, your methods should define what inclusions are required. So first thing you do in the method is declare what associations are required, then pass that to your extension method. Is that what your after?

Thanks for this - I will try it out and get back – Gary Joynes Dec 27 '10 at 9:30 @Garg Joynes - no problems, let me know if you have any issues. – RPM1984 Dec 27 '10 at 21:37 @Gary Joynes - any luck? – RPM1984 Jan 22 at 4:07 @RPM1984 - sorry for the delay.

The solution is perfect, many thanks. I also used the solution from stackoverflow. Com/questions/632434/… to handle Where clauses in the same way – Gary Joynes Mar 12 at 7:32 1 @MvcCmsJon - keep in mind, if your on the new version of EF, there is strongly-typed Include, so this is no longer required.

– RPM1984 May 17 at 3:35.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions