3 回答
TA贡献1995条经验 获得超2个赞
您的请求与您的对象不匹配,您是否希望在所有部分中都有一个结合 Message 和 Question 的唯一列表?
或者,如果您想要像“Section => Questions => Messages”这样的树,您可以这样做:
var group1 = SectionList.Select(section => new Section
{
SectionID = section.SectionID,
SectionName = section.SectionName,
Questions = QuestionList.Where(question => question.SectionID.Equals(section.SectionID))
.Select(question => new Question
{
QuestionID = question.QuestionID,
QuestionName = question.QuestionName,
SectionID = section.SectionID,
Messages = MessagesList.Where(message => message.QuestionID.Equals(question.QuestionID))
.Select(message => new Messages
{
MessagesID = message.MessagesID,
MessagesName = message.MessagesName,
QuestionID = message.QuestionID
})
})
});
当然,你需要稍微改变一下你的类:
public class Question
{
public int QuestionID { get; set; }
public string QuestionName { get; set; }
public int SectionID { get; set; }
public IEnumerable<Messages> Messages { get; set; }
}
public class Section
{
public int SectionID { get; set; }
public string SectionName { get; set; }
public IEnumerable<Question> Questions { get; set; }
}
TA贡献1803条经验 获得超6个赞
您可以加入QuestionListinto MessagesList。QuestionMessagesList之后,您需要QuestionMessagesList加入SectionList。
var questionMessages = QuestionList.GroupJoin(MessagesList,
q => q.QuestionID,
m => m.QuestionID,
(Question, Messages) => new { Question, Messages }
);
var groupJoin = SectionList.GroupJoin(questionMessages,
sct => sct.SectionID,
s => s.Question.SectionID,
(sct, QuestionsGroup) => new
{
Questions = QuestionsGroup,
SectionName = sct.SectionName
});
然后:
foreach (var item in groupJoin)
{
Console.WriteLine(item.SectionName);
foreach (var stud in item.Questions)
{
Console.WriteLine($" - {stud.Question.QuestionName}");
foreach (var message in stud.Messages)
{
Console.WriteLine($" -- {message.MessagesName}");
}
}
}
TA贡献1775条经验 获得超11个赞
您的创建结构应该改变,但如果您想要一个保留大部分创建结构的解决方案,您可以使用这个
但请注意,在每个问题和每个部分中使用 where 并不利于性能。您应该在创建部分列表时创建消息列表和问题列表。
IList<Messages> MessagesList = new List<Messages>() {
new Messages(){ MessagesID = 1, MessagesName="Message 1",QuestionID=1},
new Messages(){ MessagesID = 2, MessagesName="Message 2",QuestionID=1},
new Messages(){ MessagesID = 3, MessagesName="Message 3",QuestionID=2}
};
// Question collection
IList<Question> QuestionList = new List<Question>() {
new Question() { QuestionID = 1, QuestionName = "q1", SectionID = 1,messages = MessagesList.Where(message=> message.QuestionID==1).ToList() } ,
new Question() { QuestionID = 2, QuestionName = "q2", SectionID = 1,messages = MessagesList.Where(message=> message.QuestionID==2).ToList() } ,
new Question() { QuestionID = 3, QuestionName = "q3", SectionID = 2,messages = MessagesList.Where(message=> message.QuestionID==3).ToList() } ,
new Question() { QuestionID = 4, QuestionName = "q4" , SectionID = 2 ,messages = MessagesList.Where(message=> message.QuestionID==4).ToList()} ,
new Question() { QuestionID = 5, QuestionName = "q5" , SectionID = 2 ,messages = MessagesList.Where(message=> message.QuestionID==5).ToList() }
};
IList<Section> SectionList = new List<Section>() {
new Section(){ SectionID = 1, SectionName="Section 1",questions = QuestionList.Where(question=> question.SectionID == 1).ToList()},
new Section(){ SectionID = 2, SectionName="Section 2",questions = QuestionList.Where(question=> question.SectionID == 2).ToList()},
new Section(){ SectionID = 3, SectionName="Section 3",questions = QuestionList.Where(question=> question.SectionID == 3).ToList()}
};
foreach (var section in SectionList)
{
Console.WriteLine("Section: " + section.SectionName);
foreach (var question in section.questions)
{
Console.WriteLine("\tQuestion: " + question.QuestionName);
foreach (var message in question.messages)
{
Console.WriteLine("\t\tMessage: " + message.MessagesName);
}
}
}
为此,您必须稍微更改您的课程。
public class Question
{
public int QuestionID { get; set; }
public string QuestionName { get; set; }
public int SectionID { get; set; }
public List<Messages> messages { get; set; }
}
public class Messages
{
public int MessagesID { get; set; }
public string MessagesName { get; set; }
public int QuestionID { get; set; }
}
public class Section
{
public int SectionID { get; set; }
public string SectionName { get; set; }
public List<Question> questions { get; set; }
}
- 3 回答
- 0 关注
- 179 浏览
添加回答
举报