我對於在實體框架中使用lambda表達式很感興趣,並且在這方面會很感謝。如何在實體框架中使用lambda表達式和謂詞
在我的項目,我有兩個實體作者和課程與1-M的關係(一個作者可以有多個課程):
public class Author
{
public int AuthorId {get; set;}
public string Name {get; set;}
public virtual ICollection<Course> Courses {get; set;}
}
public class Course
{
public int CourseId {get; set;}
public string Title {get; set;}
public int AuthorId {get; set;}
public virtual Author {get; set;}
}
我要與相關的課程檢索所有的作者 - 這同樣適用於我的項目中的其他實體。出於這個原因,我想類似於get()方法如下:
public class GenericService<T> where T : class
{
// DbContext defined
public IEnumerable<T> Get(...)
{
return Context...
}
}
在我的控制器動作,我會做類似下面的東西:
public ActionResult Get_Authors_With_Courses()
{
// var authors = Get (...)
return View(authors);
}
我不知道如何定義Get(...)返回父/子對象的方法。有人可以在這方面指導我。
在此先感謝。
閔採爾,這工作就像一個魅力。這就是我一直在尋找的東西,尤其是通用零件。有沒有辦法一次加載所有的Child對象?我認爲對於這種情況,我們必須編寫單獨的查詢。 – Tech
我認爲一個更好的做法是啓用延遲加載[閱讀此](http://www.entityframeworktutorial.net/EntityFramework4.3/lazy-loading-with-dbcontext.aspx),這種方式應該工作。 – Munzer
我很高興我幫助:) – Munzer