Menü Schließen

Load Multiple Levels in Entity Framework using Linq.

When you use the Entity Framework to query data from a database, you often have to load detail data related to a database entry. Child elements can easily be obtained from the database with LINQ.

Loading direct children:

var steps = from b in db.Steps.Include(s => s.Questions) 
            orderby b.StepOrder 
            select b;

By using the include expression, the direct children of a step are also loaded and included in the SQL select statement.

Loading grand children:

var steps = from b in db.Steps.Include(s => s.Questions.Select(q => q.Answers))
            orderby b.StepOrder
            select b;

This can be obtained by cascading a select in a include statement.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert