我有一个名为博客的模型:
public class Blog
{
[Key]
public int blogId { get; set; }
[Required]
public string blogTitle { get; set; }
public string imagePath { get; set; }
[Required, DataType(DataType.Html)]
public string blogDescription { get; set; }
[DataType(DataType.DateTime)]
public DateTime blogDateTime { get; set; }
[Required]
public bool isPublished { get; set; }
[Required]
public BlogCategory blogCategory { get; set; }
public List<Comments> blogComments { get; set; }
}
和另一个模型评论:
public class Comments
{
[Key]
public int authorId { get; set; }
[Required]
public string commentAuthor { get; set; }
[DataType(DataType.Date)]
public DateTime commentDate { get; set; }
public bool isHidden { get; set; }
[Required, DataType(DataType.EmailAddress)]
public string commentAuthorEmail { get; set; }
[Required]
public string commentDescription { get; set; }
public Blog Blog { get; set; }
}
在Blogs控制器中,我要访问isHidden = false
我尝试过的是:
var blog = await _context.Blog
.Include(cat => cat.blogCategory)
.Include(comments => comments.blogComments.Any(c => !c.isHidden))
.FirstOrDefaultAsync(m => m.blogId == id);
但我得到的唯一例外是
An unhandled exception occurred while processing the request.
InvalidOperationException: The Include property lambda expression 'comments => comments.blogComments.Find(c => Not(c.isHidden))' is invalid. The expression should represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, specify an explicitly typed lambda parameter of the target type, E.g. '(Derived d) => d.MyProperty'.
我该如何解决这个问题?
答案 0 :(得分:0)
function addHandlers() {
for (i = 0; i < squaresPerSide * squaresPerSide; i++) {
console.log(document.getElementsByClassName('boxes')[i]);
document.getElementsByClassName('boxes')[i].addEventListener("mouseover",function (event){ //Eventlistener to change the background color when mouse over is done.
//console.log(event);
event.target.style.backgroundColor = 'green';
}
);
}
}
由于它是一个IQueryable,因此结果相同。它只会返回隐藏博客评论的评论。考虑到您要返回所有博客,而不仅仅是返回其注释被隐藏的博客。
答案 1 :(得分:0)
您无法在EF核心的Include
中进行过滤,请尝试使用以下代码代替
var blog = await _context.Blog
.Include(cat => cat.blogCategory)
.Include(comments => comments.blogComments)
.FirstOrDefaultAsync(m => m.blogId == id);
blog.blogComments = blog.blogComments.Where(b => b.isHidden == false).ToList();
答案 2 :(得分:0)
您必须使用实体框架投影。
var blog = await _context.Blog
.Select(p => new Blog
{
blogComments = p.blogComments.Where(s => !s.isHidden )
}.FirstOrDefaultAsync(m => m.blogId == id);
答案 3 :(得分:-1)
布尔值true为false,因此请尝试: //未测试
(comments => comments.blogComments.where(c=>c.isHidden==false))