C# ASP.NET Core Web API 包含在哪里
C# ASP.NET Core Web API include with 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)
.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();
}
所以当我询问评论并回复 return confirmation == true 时,我收到以下错误。
错误:Include 中使用的 Lambda 表达式无效。
我该如何解决这个问题?
您应该将 属性 名称作为字符串传递。
喜欢 .Include("类别")
您可以在此处查看该方法的重载:https://docs.microsoft.com/en-us/dotnet/api/system.data.objects.objectquery-1.include?view=netframework-4.8
恐怕 filtered include 仅受 EF Core v5.0.0 的支持,目前在 preview
中。
如果您使用的是较旧的 EF Core 版本,您可以查看 this thread 中的一些想法。
例如,如果您能承受在内存中提取所有内容和过滤的性能影响,您可以尝试执行以下操作:
var blog = _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();
blog.Comments = blog.Comments.Where(comment => comment.Confirmation == true).ToList();
foreach(var comment in blog.Comments)
{
comment.Replies = comment.Replies.Where(reply => reply.Confirmation == true).ToList();
}
return blog;
我有这样的代码:
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();
}
所以当我询问评论并回复 return confirmation == true 时,我收到以下错误。
错误:Include 中使用的 Lambda 表达式无效。
我该如何解决这个问题?
您应该将 属性 名称作为字符串传递。
喜欢 .Include("类别")
您可以在此处查看该方法的重载:https://docs.microsoft.com/en-us/dotnet/api/system.data.objects.objectquery-1.include?view=netframework-4.8
恐怕 filtered include 仅受 EF Core v5.0.0 的支持,目前在 preview
中。
如果您使用的是较旧的 EF Core 版本,您可以查看 this thread 中的一些想法。
例如,如果您能承受在内存中提取所有内容和过滤的性能影响,您可以尝试执行以下操作:
var blog = _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();
blog.Comments = blog.Comments.Where(comment => comment.Confirmation == true).ToList();
foreach(var comment in blog.Comments)
{
comment.Replies = comment.Replies.Where(reply => reply.Confirmation == true).ToList();
}
return blog;