【发布时间】:2020-04-14 00:54:16
【问题描述】:
我想实现一个页面,其中每个类别都有其相关文章,我已实现如下:
ArticlesHomeViewModel articles = new ArticlesHomeViewModel
{
category1 = db.Articles.Include(o => o.Category).Where(m => m.category == "Category One Name").limit(4),
category2 = db.Articles.Include(o => o.Category).Where(m => m.category == "Category two Name").limit(4),
category3 = db.Articles.Include(o => o.Category).Where(m => m.category == "Category Three Name").limit(4),
category4 = db.Articles.Include(o => o.Category).Where(m => m.category == "Category Four Name").limit(4),
}
我想将它们合并到一个查询中,以便能够在视图中对它们进行循环,因为我不知道系统中有多少类别。
【问题讨论】:
-
既然不知道有多少类。我认为您应该使用
GroupBy查询-> stackoverflow.com/questions/7325278/group-by-in-linq -
请看看我的编辑你有语法错误。
标签: c# asp.net-mvc asp.net-core entity-framework-core