0
我有这个挣扎了一段时间...后获得数评论实体框架
我要实现对已经放完了集博客文章的每一个博客评论的计数值。
public class Blog
{
public int BlogId { get; set; }
public string BlogPostName { get; set; }
// ... //
public virtual ICollection<BlogPostComments> BlogPostComments { get; set; }
}
public class BlogPostComments
{
public int BlogPostCommentsId { get; set; }
public string NameOfUser { get; set; }
public string CommentsOfUser { get; set; }
public Nullable<System.DateTime> PostedDate { get; set; }
public Nullable<int> BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
他们在一对多的关系-1 BlogPost可以有0或更多的BlogPostComments。 例如,我有一个整数数组int [] ids的输入,其中包含BlogId 1,2和3.我想以字典格式获取与这些ID注释相关的所有内容。 此刻我真的能得到评论的数量为所有博客帖子与此
public IHttpActionResult Get([FromUri] int[] ids)
{
var blogPosts = (from blogPost in EntityContext.BlogPostComments
where ids.Contains(customer.BlogId.Value)
select blogPost);
int numberOfAllComments = blogPosts.Count();
return Ok(numberOfAllComments);
}
让我们说 - 第一个博客帖子有3条评论,博客帖子第二有5个意见和第三的博文有7条评论。
有没有办法将第一篇文章的评论和第二篇文章的评论分开,并将它们作为与Entity Framework的关键值对返回。请帮助我构建或修改我的查询。
我想先说感谢你很多它像一个魅力的工作!!!!能否请你给我解释一点点是什么请在这个和平的代码后面流动 – NDym