I am working on a Asp.net core 2.2 project and have a problem about loading related data in ViewModel.
First of all i have a table named QuestionTbl :
public class Question
{
[Key]
public int questionID { get; set; }
public string questionTitle { get; set; }
public string GroupID { get; set; }
}
As you see i have a string property called GroupID in Question table that shows groups of each question.
For Example
1 row in questionTbl
---------
questionID = 1
questionTitle = 'What is Your Name ?'
GroupID = '3,4,5'
In the example above question with ID = 1 is in 3 groups (3 and 4 and 5).
And GroupTbl :
public class Group
{
[Key]
public int GroupID { get; set; }
public string GroupName { get; set; }
}
Now i want to show list of question with related groups.
I have a ViewModel like this :
public class GetQuestionViewModel
{
public int questionID { get; set; }
public string questionTitle { get; set; }
public ICollection<Group> QuestionGroups { get; set; }
}
And my entity framework query :
var questionQuery = (from a in _db.QuestionTbl
select new GetQuestionViewModel()
{
questionID = a.questionID,
questionTitle = a.questionTitle
})
.Include(q => q.QuestionGroups)
.ToList();
I want to have a list contains questions and groups of each question. But QuestionGroups returns null in my query. I also read this link but it did not help me.