[英]Entity Framework 4 - Include self-related table
我有一張與自我相關的表格:
UnitID UnitParentID Name
檢索1級代碼:
return contexto.unit
.Include("unit1")
檢索2個級別的代碼:
return contexto.unit
.Include("unit1.unit1")
檢索3個級別的代碼:
return contexto.unit
.Include("unit1.unit1.unit1")
我如何在多個級別執行此操作?
這些天我遇到了這個問題並且像這樣解決了。
你必須首先加載所有的entites像:
List<unit> myUnits = (from o in ctx.unit
.Expand("units")
select o).ToList();
之后,你必須選擇你想要的這些單位:
var selectedUnits = myUnits.Where(u => u.Property == x).ToList();
這對我來說很好! 希望我能幫助你!
最好的問候朱利安
簡短的回答:你沒有。
更長的答案:您向單元添加一個額外的列以識別單元屬於一起。 然后你做的事情如下:
var tempResult = myDataContext.unit.Where(x => x.id == id);
return tempResult.FirstOrDefault(); //or some other logik to return the correct 'first' unit.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.