通过3个列表返回一个对象和一个对象

本文关键字:一个对象 返回 列表 3个 通过 | 更新日期: 2023-09-27 18:18:34

我有一个查询,它给了我一个带有类别的测验,有一个问题和一个答案,如下面的查询所示。但是,我希望只使用一个阅读器,并且希望它尽可能面向对象,这意味着我希望Quiz是根对象。所有的东西都要通过小测验。

我如何返回一个测验与类别列表,与另一个问题列表和另一个答案列表?

下面的代码:

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("answersDesc"));
                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; }
        }

通过3个列表返回一个对象和一个对象

我认为你的问题是你失去了值,因为你在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);
            }
相关文章: