Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将相关实体嵌套在linq to实体中?调查问题、子问题和选项_C#_Linq_Linq To Entities - Fatal编程技术网

C# 如何将相关实体嵌套在linq to实体中?调查问题、子问题和选项

C# 如何将相关实体嵌套在linq to实体中?调查问题、子问题和选项,c#,linq,linq-to-entities,C#,Linq,Linq To Entities,我对LINQtoSQL和LINQtoEntities是完全陌生的。我设置了一个返回json的服务。我很难找到如何返回嵌套或投影内容(不确定需要哪一个)。现在我有一个问题,数据返回良好,没有嵌套 这是我的密码 public List<Question> GetQuestionsByGymID(string gymID) { // Question class is translated structure from GymQuestionEntities List<

我对LINQtoSQL和LINQtoEntities是完全陌生的。我设置了一个返回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;
}

要包含子实体,可以使用“包含”方法:


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

在包含方面遇到问题。我将更新代码。json部分工作正常。更新的代码显示了我尝试使用的内容。Include函数(第二种方法)。另外,我在这里使用一个黑盒,因为它不会出现在WCF测试客户端中。调试的最佳方法是从我的服务中删除WebInvoke方法并在测试客户端中工作吗?通过使用断点,我可以看到我的GetQuestionsWithoOptions ByGymid方法返回QuestionWithOptions项,这些选项项与包含的QuestionOptions一起添加到列表中,但仍然显示为空。
{
    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
},