C# 实体框架:加载筛选的集合

C# 实体框架:加载筛选的集合,c#,.net,linq,entity-framework,domain-driven-design,C#,.net,Linq,Entity Framework,Domain Driven Design,给定下一个模型 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 &g

给定下一个模型

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();
如您所知,它将抛出异常,因为在LINQtoEntities中使用了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();
它工作正常,但我想使用我设计的作者界面

你知道我可以用什么方法来维护对象设计吗?

可能很简单,我在开始使用EF时也有同样的想法