如何将linq中的相关实体嵌入到实体中?调查问题,子问题和选项

时间:2014-02-21 14:56:28

标签: c# linq linq-to-entities

我是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
},

1 个答案:

答案 0 :(得分:0)

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

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