繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM