Я пытаюсь создать список списков, где большой список представляет собой бумагу, содержащую коллекцию небольшого списка, представляющего вопрос, список вопросов состоит из строки вопроса и его идентификатора. вот мой код:
public class Genes
{
public string question { get; set; }
public int CLO { get; set; }
}
List<Genes> questiongene = new List<Genes>();
List<List<questiongene>> paper = new List<List<questiongene>>();
теперь я составляю список вопросов без ошибок, но когда я пытаюсь создать более крупный список, Visual Studio не может распознать переменный тип вопроса как тип, где не так?
Как насчет: List <List <Genes>> paper = new List <List <Genes>> () ;?
Почему вы не создаете класс QuestionPaper со свойством List<Questiongene> Questions?
Вы создаете список типа, а не список объектов, что вы пытаетесь сделать здесь
Возможный дубликат Создание списка списков в C#
Измените свою структуру так, чтобы сказать, что у вас есть классы A и B. Класс A может иметь свойство списка B, а затем вы можете иметь список A. List<A> mainList, где каждый экземпляр A имеет список B. это то, что было @TimSchmelter ссылаясь на





Я бы сделал это, а не базовый List<List<Genes>>, как предлагалось, так как тогда ясно, что представляет собой каждый элемент ...
//This is your class for a single question. A list of these will
//make up a paper.
public class Genes
{
public string Question{ get; set; }
public int CLO { get; set; }
}
//This is a Paper, which contains a list of Questions.
public class Paper
{
public List<Genes> Questions { get; set; }
}
..
..
//To use: First create a list of papers -
//this is a stack of papers.
List<Paper> stackOfPapers = new List<Paper>();
//now create a list of questions which we can add to a paper
List<Genes> questions = new List<Genes>();
//Now we need to create a question to add to the list of questions.
Genes newQuestion = new Genes();
newQuestion.Question = "How many roads must a man walk down?";
newQuestion.CLO = 42;
//now add the question to the list.
questions.Add(newQuestion);
//now we need to create a paper. This will represent one
//paper in our stack of papers.
Paper newPaper = new Paper();
//add our list of Questions to the paper.
newPaper.Questions = questions;
//and finally, add the paper to the stack of papers.
stackOfPapers.Add(newPaper);
//or alternatively, you can use the object initializer syntax to
//do this all in one. Note I've also added more than one paper to
//the list of papers, and each paper has two questions contained in it:
var newStackOfPapers =
new List<Paper>
{
new Paper
{
Questions = new List<Genes> {
new Genes
{
Question = "How many roads must a man walk down?",
CLO = 42
},
new Genes
{
Question = "Another Question?",
CLO = 111
}
}
},
//add Another paper...
new Paper
{
Questions = new List<Genes> {
new Genes
{
Question = "This is the first question on the second paper?",
CLO = 22
},
new Genes
{
Question = "Another Question?",
CLO = 33
}
}
},
};
Он создает то, что он хочет. Переменная, содержащая стопку бумаг, каждая из которых содержит список вопросов.
@MongZhu Теперь лучше? Я признаю, что, вероятно, предполагал слишком большую способность просто понимать код от имени OP, основываясь на его первоначальном вопросе. Теперь все должно быть намного яснее.
В вашем коде нет типа questiongene, только тип Genes. Создайте список списков:
List<List<Genes>> paper = new List<List<Genes>>();
или создайте новый тип бумаги с генератором вопросов List:
public class Paper
{
public List<Genes> questionGenes { get; set; }
// default constructor
public Paper()
{
questionGenes = new List<Genes>();
}
// constructor that creates a paper from a List<Genes>
public Paper(List<Genes> questionGene)
{
questionGenes = questionGene;
}
}
а затем создайте список Paper:
List<Paper> papers = new List<Paper>();
papers.Add(new Paper(questiongene));
Список <List <Genes>> бумаги ...