简体   繁体   English

如何获得在linq中嵌套到实体的相关实体? 调查问题,子问题和选项

[英]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.

相关问题 我如何获得一个实体及其所有相关实体 - How do I get an entity and all of its related entities 如何使用LINQ在数据库中的调查中找到带有答案的问题数量 - How do I find the number of questions with answers in a survey in my database using LINQ 如何删除与特定实体相关的所有实体? - How do I delete all the entities related to a specific entity? 如何在 Entity Framework LINQ To Entities 中进行联合? - How can I do a Union all in Entity Framework LINQ To Entities? LINQ to Entities-将所有相关实体字段转换为字符串 - LINQ to Entities - get all related entity field into a string 如何使用AutoMapper更新具有嵌套实体的实体,并使用实体框架保存更新的实体? - How do I update an entity with nested entities with AutoMapper and save the updated Entity with Entity Framework? 如何使用 EF 和 linq 获取包含嵌套信息的实体 - How can I get this entity with nested information using EF and linq 如何进行EF Linq查询,包括相关实体的子集 - How can I do an EF Linq query, including a subset of related entities LINQ to Entities无法识别该方法(在相关实体上) - LINQ to Entities does not recognize the method (on a related entity) 使用linq和相关实体查询实体框架7 - Query to entity framework 7 using linq and related entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM