Entity Framework 4 - Include Your Own Table

I have a separate table:

UnitID UnitParentID Name 

Code for level 1 extraction:

 return contexto.unit .Include("unit1") 

Code to extract 2 levels:

 return contexto.unit .Include("unit1.unit1") 

Code to extract 3 levels:

 return contexto.unit .Include("unit1.unit1.unit1") 

How to do this for many levels?

+4
source share
2 answers

I had this problem these days and solved it like that.

You must first download all rights:

 List<unit> myUnits = (from o in ctx.unit .Expand("units") select o).ToList(); 

After that, you should select these units that you want to have:

 var selectedUnits = myUnits.Where(u => u.Property == x).ToList(); 

This works great for me! Hope I can help you!

Regards, Julian

+2
source

The short answer is no.

Longer answer: you add an extra column to the block to identify a unit that belongs together. Then you do something like:

 var tempResult = myDataContext.unit.Where(x => x.id == id); return tempResult.FirstOrDefault(); //or some other logik to return the correct 'first' unit. 
+1
source

Source: https://habr.com/ru/post/1382696/


All Articles