【发布时间】:2018-06-07 08:18:51
【问题描述】:
我有 2 张桌子。
Department | Questions
ID Name | DepartmentID QTitle QDescription
1 a | 1 aaa bbb
2 b | 1 ddd ccc
3 c | 2 eee fff
| 2 ggg hhh
| 我想用 ViewModel 对数据进行分组并在 View 中显示。我可以使用 QTitle 获取分组数据,但 QDescription 不能。
Linq 查询
var questions= (from s in dbContext.Questions
join b in dbContext.Department
on s.DepartmentID equals b.ID
group s.QTitle by b.DepartmentID into g
select new QuestionGroupedViewModel
{
DepartmentName= g.Key,
QTitle= g.ToList()
}).ToList();
视图模型
public class QuestionGroupedViewModel
{
public string DepartmentName{ get; set; }
public List<string> QDescription{ get; set; }
public List<string> QTitle{ get; set; }
}
【问题讨论】:
标签: asp.net-mvc entity-framework linq linq-to-sql