Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - Include multiple level properties

I am trying to get a table from database using entity framework.

The table has reference to other table which again has reference to other tables. I know how to include other tables. And according to this answer and this MSDN page including multiple levels are like this:

entity.TableLevel1.Include(tLvl1=>tLvl1.TableLevel2.Select(tLvl2=>tLvl2.TableLevel3));

But my question is, how to include another table at level 3 ?

This seems not to work:

entity.TableLevel1
          .Include(tLvl1=>tLvl1.TableLevel2
               .Select(tLvl2=>tLvl2.TableLevel3)
               .Select(tLvl2 => tLvl2.AnotherTableLevel3);
like image 942
Nawed Nabi Zada Avatar asked Feb 26 '16 15:02

Nawed Nabi Zada


1 Answers

Add another Include call:

entity.TableLevel1.Include(tLvl1=>tLvl1.TableLevel2.Select(tLvl2=>tLvl2.TableLevel3))
                  .Include(tLvl1=>tLvl1.TableLevel2.Select(tLvl2=>tLvl2.AnotherTableLevel3));

If you want to load related entities that are at the same level you should call Include extension method for each of them.

like image 70
octavioccl Avatar answered Oct 26 '22 01:10

octavioccl