[英]How do I get related entity nested in linq to entities? Survey questions, subquestions, and options
I am completely new to LINQ-to-SQL and LINQ-to-Entities. 我对LINQ-to-SQL和LINQ-to-Entities完全陌生。 I have a service set up that returns json.
我有一个返回json的服务设置。 I am having trouble finding out how to return nested or projected content (not sure which one I need).
我在寻找如何返回嵌套或投影内容时遇到麻烦(不确定我需要哪一个)。 Right now I have the question data returning fine with no nesting.
现在,我的问题数据没有嵌套也可以正常返回。
This is my code. 这是我的代码。
public List<Question> GetQuestionsByGymID(string gymID)
{
// Question class is translated structure from GymQuestionEntities
List<Question> questionList = new List<Question>();
int intid;
bool result = int.TryParse(gymID, out intid);
using (var context = new SurveyEntities())
{
var questionEntity =
from p in context.GymQuestionsEntities
join o in context.QuestionTypeEntities
on p.QuestionType equals o.ID
join n in context.SurveyQuestionEntities
on p.Question equals n.ID
where p.Gym == intid
select new Question
{
ID = p.ID,
Gym = p.Gym,
Section = p.Section,
QuestionSequence = p.QuestionSequence,
Instruction = p.Instruction,
QuestionType = o.Type,
QuestionID = p.Question,
QuestionText = n.QuestionText,
parentQuestion = p.parentQuestion,
isRequired = p.isRequired,
isMatrix = p.isMatrix,
MatrixFloor = p.MatrixFloor,
MatrixCeiling = p.MatrixCeiling,
};
foreach (var Entity in questionEntity)
{
if (Entity != null)
questionList.Add(Entity);
}
}
return questionList;
}
// this method returns empty page. I have no idea how to debug this.
public List<QuestionWithOptions> GetQuestionsWithOptionsByGymID(string gymID)
{
int intid;
bool result = int.TryParse(gymID, out intid);
List<QuestionWithOptions> questionList = new List<QuestionWithOptions>();
List<Question> Questions = GetQuestionsByGymID(gymID);
using (var context = new UTourEntities())
{
var questionWithOption = from p in context.GymQuestionsEntities.Include("QuestionOptions").Include("OptionChoices")
join o in context.QuestionTypeEntities
on p.QuestionType equals o.ID
join n in context.SurveyQuestionEntities
on p.Question equals n.ID
where p.Gym == intid
select new QuestionWithOptions
{
ID = p.ID,
Gym = p.Gym,
Section = p.Section,
QuestionSequence = p.QuestionSequence,
Instruction = p.Instruction,
QuestionType = o.Type,
QuestionID = p.Question,
QuestionText = n.QuestionText,
parentQuestion = p.parentQuestion,
isRequired = p.isRequired,
isMatrix = p.isMatrix,
MatrixFloor = p.MatrixFloor,
MatrixCeiling = p.MatrixCeiling,
QuestionOptions = p.QuestionOption
};
foreach (var Entity in questionWithOption)
{
if (Entity != null)
questionList.Add(Entity);
}
}
return questionList;
// StackOverflow_Answer
// return null;
}
This is what my entities look like: 这是我的实体的样子:
Here is hand-jammed example of what I am trying to return.
这是我要退回的东西的例子。 I took a shortcut and included OptionChoiceEntity data (the text of the option) into the QuestionOptionEntity (maps questions to options) object.
我采用了一种快捷方式,并将OptionChoiceEntity数据(选项的文本)包含在QuestionOptionEntity(将问题映射到选项)对象中。
{
ID: 4,
Gym: 8,
Section: 1,
QuestionSequence: 4,
QuestionType: "Heading",
parentQuestion: null,
QuestionID: 4,
QuestionText: "What are some areas of interest?",
Instruction: null,
ChildQuestions:
{
ID: 5,
Gym: 8,
Section: 1,
QuestionSequence: 5,
QuestionType: "Checkbox",
parentQuestion: 4,
QuestionID: 5,
QuestionText: "Aquatics",
Instruction: null,
QuestionOption:
{
ID: 2,
Question: 5,
OptionSequence: 1,
OptionChoice: "Swimming",
isOther: false
},
{
ID: 3,
Question: 5,
OptionSequence: 2,
OptionChoice: "Lessons",
isOther: false
}
isRequired: false,
isMatrix: false,
MatrixFloor: null,
MatrixCeiling: null
},
QuestionOption:
{
ID: 1,
Question: 4,
OptionSequence: 1,
OptionChoice: "Other",
isOther: true
}
isRequired: false,
isMatrix: false,
MatrixFloor: null,
MatrixCeiling: null
},
To include the subentities you can use the "Include" method : http://msdn.microsoft.com/en-us/library/bb738708(v=vs.110).aspx 要包含子实体,可以使用“包含”方法: http : //msdn.microsoft.com/zh-cn/library/bb738708( v=vs.110) .aspx
Regarding the end json result, you could mark your entities as being Serializable and use DataContractJsonSerializer to serialize them as json. 关于最终的json结果,您可以将您的实体标记为可序列化,并使用DataContractJsonSerializer将其序列化为json。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.