[英]Entity Framework: Loading Filtered Collections
給定下一個模型
public class Author{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Book> Books { get; set; }
public IQueryable<Book> GetGoodBooks(){
return Books.Where(n => n.Rating > 6)
}
}
public class Book{
public int Id { get; set; }
public string Name { get; set; }
public int Rating { get; set; }
public Author Author { get; set;}
}
我需要檢索( 僅一個查詢 )所有作者及其所有好書(使用GetGoodBooks() )。 所以我下一個:
IQueryable<Author> query = from author in repository.ListAll<Author>() // it returns an IQueryable<Author>
select new Author() {
author.Id,
author.Name,
Books = author.GetGoodBooks() // it throws an exception
};
var list = query.ToList();
如您所知,由於在Linq to Entities中使用方法GetGoodBooks() ,它將引發異常。
因此,我知道我可以使它替換Linq子句的方法,如下所示:
IQueryable<Author> query = from author in repository.ListAll<Author>() // it returns an IQueryable<Author>
select new Author() {
author.Id,
author.Name,
Books = author.Books.Where(n => n.Rating > 6)
};
var list = query.ToList();
可以,但是我想使用我設計的作者界面!
您知道我可以通過哪種方式維護對象設計嗎?
在LINQ to Entities中使用自定義方法/擴展方法的可能的雙重解決方法當我開始使用EF時,我有相同的想法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.