实体框架:多对多计数和求和

时间:2022-10-05 21:03:26

I am using the Entity Framework. I have a many to many relationship Articles <-> Categories. Each article can belong to many categories, and each category can belong to more than one article. I can get the COUNT of articles within each category:

我正在使用实体框架。我有很多关系文章< - >类别。每篇文章都属于多个类别,每个类别可以属于多个文章。我可以在每个类别中获得COUNT个文章:

public IEnumerable<MenuCategory> GetMenuCategories()
{
   return (from c in db.CategorySet.Include("Articles")
     orderby c.CategoryName
     select new MenuCategory
     {
        CategoryID = c.CategoryID,
        CategoryName = c.CategoryName,
        CountOfCategory = c.Articles.Count()
      });

}

But I also need to get the total number of articles so that I can calculate what percentage of articles is in each category. Can anyone tell me how to do that by extending the code above and without making a separate call to the database?

但我还需要获得文章总数,以便我可以计算每个类别中文章的百分比。谁能告诉我如何通过扩展上面的代码而不单独调用数据库来做到这一点?

Thanks

1 个解决方案

#1


The code needs to know the total number of articles in order to do this.

代码需要知道文章的总数才能做到这一点。

If you want to avoid a second database call then you can do the calc in the database and return it as an extra column in the result set. There are different ways to acheive this but one would be to use a view and have a SQL function TotalArticles, then your calc is just articleCount / TotalArticles. I don't know much about the entity-framework so I'm not sure if this is workable.

如果要避免第二次数据库调用,则可以在数据库中执行计算并将其作为结果集中的额外列返回。有不同的方法来实现这个,但一个是使用视图并具有SQL函数TotalArticles,那么你的calc只是articleCount / TotalArticles。我对实体框架了解不多,所以我不确定这是否可行。

If you can't do the calc in the database then I imagine the second database call is unavoidable although it is just a count of articles so hardly adding much overhead. You could make this call before the main call and then add the value to your results in a similar manner to above, i.e. PercentageOfArticles = c.Articles.Count() / articleCount

如果你不能在数据库中进行计算,那么我想第二次数据库调用是不可避免的,虽然它只是一个文章计数,所以几乎没有增加太多的开销。您可以在主调用之前进行此调用,然后以与上面类似的方式将值添加到结果中,即PercentageOfArticles = c.Articles.Count()/ articleCount

#1


The code needs to know the total number of articles in order to do this.

代码需要知道文章的总数才能做到这一点。

If you want to avoid a second database call then you can do the calc in the database and return it as an extra column in the result set. There are different ways to acheive this but one would be to use a view and have a SQL function TotalArticles, then your calc is just articleCount / TotalArticles. I don't know much about the entity-framework so I'm not sure if this is workable.

如果要避免第二次数据库调用,则可以在数据库中执行计算并将其作为结果集中的额外列返回。有不同的方法来实现这个,但一个是使用视图并具有SQL函数TotalArticles,那么你的calc只是articleCount / TotalArticles。我对实体框架了解不多,所以我不确定这是否可行。

If you can't do the calc in the database then I imagine the second database call is unavoidable although it is just a count of articles so hardly adding much overhead. You could make this call before the main call and then add the value to your results in a similar manner to above, i.e. PercentageOfArticles = c.Articles.Count() / articleCount

如果你不能在数据库中进行计算,那么我想第二次数据库调用是不可避免的,虽然它只是一个文章计数,所以几乎没有增加太多的开销。您可以在主调用之前进行此调用,然后以与上面类似的方式将值添加到结果中,即PercentageOfArticles = c.Articles.Count()/ articleCount