【发布时间】:2020-09-23 11:57:33
【问题描述】:
我有这样的代码:
public Blog GetBlogWithCategoryTagsAndCommentsWithReplies(int id)
{
return _context.Blogs
.Where(blog => blog.Id == id)
.Include(blog => blog.Category)
.Include(blog => blog.BlogTags)
.ThenInclude(blogtag => blogtag.Tag)
.Include(blog => blog.Comments)
.ThenInclude(comment => comment.User)
.Include(blog => blog.Comments)
.ThenInclude(comment => comment.Replies)
.ThenInclude(reply => reply.User)
.FirstOrDefault();
}
在这种状态下工作没有任何问题。
但是当我在我的代码中添加 where 时,当我让它喜欢时
public Blog GetBlogWithCategoryTagsAndCommentsWithReplies(int id)
{
return _context.Blogs
.Where(blog => blog.Id == id)
.Include(blog => blog.Category)
.Include(blog => blog.BlogTags)
.ThenInclude(blogtag => blogtag.Tag)
.Include(blog => blog.Comments.Where(comment=>comment.Confirmation==true))
.ThenInclude(comment => comment.User)
.Include(blog => blog.Comments)
.ThenInclude(comment => comment.Replies.Where(reply=>reply.Confirmation==true))
.ThenInclude(reply => reply.User)
.FirstOrDefault();
}
所以当我要求 cmets 并回复返回确认 == true 时,我收到以下错误。
错误:在 Include 中使用的 Lambda 表达式无效。
我该如何解决这个问题?
【问题讨论】:
-
你无法在 include 方法中执行 lambda 表达式
-
谢谢,我应该如何更改代码?
-
请不要在图片中发布您的代码,将其作为代码写入您的问题。
标签: c# .net-core entity-framework-core linq-to-entities