我有一个类叫问题.这个问题有属性QuestionID和QuestionAnswer.当在foreach中迭代此问题列表时,我必须找到.QuestionID = 12.如果我发现.QuestionID = 12,那么我必须立即为.QuestionID = 14的.QuestionAnswer =“SomeText”赋值.
我不想在里面再次迭代.QuestionId = 12’再次找到.QuestionID = 14`.
有什么办法可以直接去.QuestionID = 14使用LINQ?
例如:
For Each mQuestion As Question In _mQuestions If mQuestion.QuestionId = 12 Then 'Find mQuestion.QuestionID= 14 and insert Somtext to 'mQuestion.QuestionAnswer="SomeText" End IF Next
解决方法
我想你正在寻找这样的东西.如果我有一段时间,我会把它翻译成VB,但我想你可以跟随.
if (_mQuestions.Any(q => q.QuestionID == 12)) { Question question14 = _mQuestions.FirstOrDefault(q => q.QuestionID == 14); if (question14 != null) question14.QuestionAnswer = "Some Text"; }