我正在做一个Asp.net core 2.2
项目,在ViewModel中加载相关数据时遇到了问题。
首先,我有一个名为QuestionTbl
的表:
public class Question
{
[Key]
public int questionID { get; set; }
public string questionTitle { get; set; }
public string GroupID { get; set; }
}
如您所见,我在Question
表中有一个名为GroupID
的string
属性,它显示每个问题的分组。
例如的
1 row in questionTbl
---------
questionID = 1
questionTitle = 'What is Your Name ?'
GroupID = '3,4,5'
在上面的例子中,ID = 1
的问题在3组(3 and 4 and 5)
中。
和GroupTbl
:
public class Group
{
[Key]
public int GroupID { get; set; }
public string GroupName { get; set; }
}
现在,我想要显示相关组的问题列表。
我有一个这样的ViewModel
:
public class GetQuestionViewModel
{
public int questionID { get; set; }
public string questionTitle { get; set; }
public ICollection<Group> QuestionGroups { get; set; }
}
和我的实体框架查询:
var questionQuery = (from a in _db.QuestionTbl
select new GetQuestionViewModel()
{
questionID = a.questionID,
questionTitle = a.questionTitle
})
.Include(q => q.QuestionGroups)
.ToList();
我想要一个包含问题和每个问题组的列表。但是在我的查询中,QuestionGroups
返回null。我也阅读了this链接,但它对我没有帮助。
https://stackoverflow.com/questions/56674220
复制相似问题