gpt4 book ai didi

c# - 如何将嵌套在 linq 中的相关实体获取到实体?调查问题、子问题和选项

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:50 26 4
gpt4 key购买 nike

我对 LINQ-to-SQL 和 LINQ-to-Entities 完全陌生。我有一个返回 json 的服务设置。我无法找到如何返回嵌套或投影的内容(不确定我需要哪一个)。现在我的问题数据返回正常,没有嵌套。

这是我的代码。

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;
}

这是我的实体的样子: EntityModel这是我试图返回的手工堵塞示例。我走了一条捷径,将 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
},

最佳答案

要包含子实体,您可以使用“包含”方法:http://msdn.microsoft.com/en-us/library/bb738708(v=vs.110).aspx

关于最终的 json 结果,您可以将您的实体标记为可序列化并使用 DataContractJsonSerializer 将它们序列化为 json。

关于c# - 如何将嵌套在 linq 中的相关实体获取到实体?调查问题、子问题和选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21937738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com