Return 对象通过 3 个列表和一个对象

Return object through 3 lists and one object

我有一个查询,它给我一个带有类别的测验,有一个问题和一个答案,正如您在下面的查询中看到的那样。但是,我只想使用一个 reader,并且我希望它尽可能面向对象,这意味着我希望 Quiz 成为根对象。一切都必须通过测验。

我如何 return 一个带有类别列表、另一个问题列表和另一个答案列表的测验?

代码如下:

public Quiz GetQuizWithCategoriesWithQuestionsWithAnswers(int id)
    {
        Quiz quiz = null;
        Category category = null;
        Question question = null;
        Answer answer = null;
        dbConnection.connection.Open();
        using (SqlCommand command = dbConnection.connection.CreateCommand())
        {
            command.CommandText = "SELECT Quiz.name, Category.name AS catName, Category.id as CatId, Category.quizId, Question.[description] as questDesc, Answer.[description] as ansDesc FROM Quiz JOIN Category ON Category.quizId = Quiz.id JOIN Question ON Question.categoryId = Category.id JOIN Answer ON Answer.questionId = Question.id WHERE Quiz.id = @id";
            command.Parameters.Add("@id", SqlDbType.Int).Value = id;
            var reader = command.ExecuteReader();
            while (reader.Read())
            {
                if (quiz == null)
                {
                    quiz = new Quiz();
                    quiz.name = reader.GetString(reader.GetOrdinal("name"));
                    quiz.id = reader.GetInt32(reader.GetOrdinal("quizId"));
                }
                if (category != null)
                {
                    if (category.id != reader.GetInt32(reader.GetOrdinal("CatId")))
                    {
                        category = new Category();
                        category.id = reader.GetInt32(reader.GetOrdinal("CatId"));
                        category.name = reader.GetString(reader.GetOrdinal("catName"));
                        quiz.categories.Add(category);
                    }
                }
                else
                {
                    category = new Category();
                    category.id = reader.GetInt32(reader.GetOrdinal("CatId"));
                    category.name = reader.GetString(reader.GetOrdinal("catName"));
                    quiz.categories.Add(category);
                }
                if (question != null)
                {
                    //You'll need to get the question ID or just use the description here instead for comparison
                    if (question.description != reader.GetString(reader.GetOrdinal("questDesc")))
                    {
                        question = new Question();
                        question.description = reader.GetString(reader.GetOrdinal("questDesc"));
                        category.question.Add(question);
                    }
                }
                else
                {
                    question = new Question();
                    question.description = reader.GetString(reader.GetOrdinal("questDesc"));
                    category.question.Add(question);
                }

                answer = new Answer();
                answer.description = reader.GetString(reader.GetOrdinal("ansDesc"));
                question.Answers.Add(answer);
            }
        }
        dbConnection.connection.Close();
        return quiz;
    }

 public class Quiz
    {
        [DataMember]
        public int id { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public List<Category> categories { get; set; }
        [DataMember]
        public List<Player> players { get; set; }

        public Quiz()
        {
            categories = new List<Category>();
        }
    }

 public class Category
    {
        [DataMember]
        public int id { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public int amount { get; set; }
        [DataMember]
        public List<Quiz> quiz { get; set; }
        [DataMember]
        public List<Question> question { get; set; }

        public Category()
        { 
            question = new List<Question>();
        }
    }

    public class Question
        {

            [DataMember]
            public int id { get; set; }
            [DataMember]
            public string description { get; set; }
            [DataMember]
            public Category category { get; set; }
            [DataMember]
            public bool isAnswered { get; set; }
            [DataMember]
            public List<Answer> Answers { get; set; }

            public Question()
            {
                Answers = new List<Answer>();
            }
        }

    public class Answer
        {
            [DataMember]
            public int id { get; set; }
            [DataMember]
            public string description { get; set; }
            [DataMember]
            public Question question { get; set; }
            [DataMember]
            public bool isCorrect { get; set; }

        }

我认为您的问题在于您丢失了值,因为您在 while 循环中每次都重新声明了 Quiz 对象,因此它丢失了所有以前的值(列表)。您需要在 while 循环之外(在另一个查询和它自己的循环中)获取 Quiz 对象的基值,并为每个列表单独查询(以避免在连接中重复)

编辑:

或者只检查测验对象是否已实例化以及是否需要为类别和问题创建新对象:

using (SqlCommand command = dbConnection.connection.CreateCommand())
            {
                command.CommandText = "SELECT Quiz.name, QuizCategory.quizId, QuizCategory.categoryId, Category.name AS catName, Question.[description], Answer.[description] FROM Quiz JOIN QuizCategory ON QuizCategory.quizId = Quiz.id JOIN Category ON Category.id = QuizCategory.categoryId JOIN Question ON Question.categoryId = Category.id JOIN Answer ON Answer.questionId = Question.id WHERE Quiz.id = @id";
                command.Parameters.Add("@id", SqlDbType.Int).Value = id;
                var reader = command.ExecuteReader();
                while (reader.Read())
                {
                    if(quiz==null)
                    {
                        quiz = new Quiz();
                        quiz.name = reader.GetString(reader.GetOrdinal("name"));
                        quiz.id = reader.GetInt32(reader.GetOrdinal("quizId"));
                    }

                    if(question!=null)
                    {
                        //You'll need to get the question ID or just use the description here instead for comparison
                        if(question.id!=reader.GetInt32(reader.GetOrdinal("questionId"))
                        {
                            category.question.Add(question);
                            question = new Question();
                            question.description = reader.GetString(reader.GetOrdinal("description"));
                        }
                    }
                    else
                    {
                        question = new Question();
                        question.description = reader.GetString(reader.GetOrdinal("description"));
                    }                        

                    if(category!=null)
                    {
                        if(category.id!=reader.GetInt32(reader.GetOrdinal("categoryId"))
                        {
                            quiz.categories.Add(category);
                            category = new Category();
                            category.id = reader.GetInt32(reader.GetOrdinal("categoryId"));
                            category.name = reader.GetString(reader.GetOrdinal("catName"));
                        }
                    }
                    else
                    {
                        category = new Category();
                        category.id = reader.GetInt32(reader.GetOrdinal("categoryId"));
                        category.name = reader.GetString(reader.GetOrdinal("catName"));
                    }

                    answer = new Answer();
                    answer.description = reader.GetString(reader.GetOrdinal("description"));
                    question.Answers.Add(answer);

                }
            }
            if(question!=null)
            {
                category.question.Add(question);
            }
            if(category!=null)
            {
                quiz.categories.Add(category);
            }