2014-05-05 42 views
-1

我有一個student表中我與Id, Name, DepartmentId, CollegeId, CityId等列數據庫和Department表,College表映射到DepartmentIdCollegeIdStudent表。在多個表上使用Entity Framework中的內部連接?

現在我想通過CityId並檢索Id, Name, DepartmentName, CollegeName幫助根據CityId使用內連接相應的表。

我採取的方法FindAll()這需要在CityId作爲輸入參數和檢索數據基於CityId

public IEnumerable<StudentRegionEntity> FindAll(int CityId) 
    { 
    var totalStudents = new List<StudentRegionEntity>(); 

    foreach(var entity in this.Dbcontext.Students) 
    { 
    } 
    } 

在foreach循環我想學生名單綁定Id, Name, Department, College領域,如何能我使用連接並實現FindAll()的功能?

+0

謝謝,以後不會重複。 – sebastian

+0

使用連接會導致您添加外部的,令人困惑的實體類(您還使用StudentRegionEntity進行了哪些操作?)。只需使用導航屬性:'Student.Region.Name'。 –

回答

0
var studentEntities = this.Dbcontext.Students 
    .Include("StudentList").Where(s => s.cityId == CityId).ToList() 

這將給所有StudentEntity實體附加StudentRegionEntity的列表使用一個連接查詢到數據庫。

+0

即時消息無法理解,我應該如何使用1個連接,並且我有多個表,並且您使用了「studentlist」作爲字符串 – sebastian

+0

這是您的StudentEntity類中的某個屬性的路徑。屬性名稱StudentList。它會根據城市ID過濾SutdentEntities,同時加入StudentRegionEntity併爲每個學生實體檢索StudentRegionEntity,您可以返回 – milagvoniduak

+0

好吧,我將值賦給StudentEntitites,我如何從studentEntities中獲取值並綁定到我的列表totalStudents? – sebastian

相關問題