【发布时间】:2017-02-18 04:50:42
【问题描述】:
我已经为此苦苦挣扎了一段时间......
我必须为已放在多组博客文章上的每个博客评论实施计数。
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 cmets 相关的所有内容。 目前,我可以通过此获取所有 BlogPost 的评论数量
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);
}
假设 - 第一个 BlogPost 有 3 个 cmets,第二个 BlogPost 有 5 个 cmets,第三个 BlogPost 有 7 个 cmets。
有没有办法将第一篇文章的 cmets 和第二篇文章的 cmets 分开,并使用实体框架将它们作为键值对返回。请帮助我构建或修改我的查询。
【问题讨论】:
标签: c# .net entity-framework