【发布时间】:2015-12-21 11:18:06
【问题描述】:
我已经看到了一些类似问题的答案,但是我似乎无法弄清楚如何将答案应用于我的问题。
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,
但我就是想不通。
我不想在最后的 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; }
}
【问题讨论】:
-
你能告诉我们
Posts架构吗? -
@DarkKnight - 见编辑
-
@grayson 你要求做的事情是不可能的。
Linq2Sql会将您的代码转换为原始的SQL,并通过连接返回子行。你不能在SQL中进行这种有条件的加入。您唯一的选择是删除.Include(aa => aa.Attachments),并进行第二个查询,根据所有者是否是作者/贡献者来返回附件。
标签: c# entity-framework linq linq-to-entities