实体框架中的条件 Include() [重复]

2024-03-01

我已经看到了类似问题的一些答案,但是我似乎无法弄清楚如何将答案应用于我的问题。

var allposts = _context.Posts
            .Include(p => p.Comments)
            .Include(aa => aa.Attachments)
            .Include(a => a.PostAuthor)
            .Where(t => t.PostAuthor.Id == postAuthorId).ToList();

附件可由作者(作者类型)或贡献者(贡献者类型)上传。我想做的只是获取附件所有者为作者类型的附件。

我知道这不起作用并给出错误:

.Include(s=>aa.Attachments.Where(o=>o.Owner is Author))

我在这里阅读了有关过滤投影的内容

编辑-文章链接: :http://blogs.msdn.com/b/alexj/archive/2009/10/13/tip-37-how-to-do-a-conditional-include.aspx http://blogs.msdn.com/b/alexj/archive/2009/10/13/tip-37-how-to-do-a-conditional-include.aspx,

但我就是无法理解它。

我不想在最后的 where 子句中包含过滤器,因为我想要所有帖子,但我只想检索属于作者的那些帖子的附件。

编辑 2: - 请求发布架构

public abstract class Post : IPostable
{

    [Key]
    public int Id { get; set; }

    [Required]
    public DateTime PublishDate { get; set; }

    [Required]
    public String Title { get; set; }

    [Required]
    public String Description { get; set; }

    public Person PostAuthor { get; set; }
    public virtual ICollection<Attachment> Attachments { get; set; }
    public List<Comment> Comments { get; set; }
}

EF Core 5.0 即将推出过滤包含。

var blogs = context.Blogs
    .Include(e => e.Posts.Where(p => p.Title.Contains("Cheese")))
    .ToList();

参考: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew#filtered-include https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew#filtered-include

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

实体框架中的条件 Include() [重复] 的相关文章

随机推荐