linq question: querying nested collections

前端 未结 4 359
囚心锁ツ
囚心锁ツ 2020-12-04 14:24

I have a Question class that has public List property that can contain several Answers.

I have a question repository which is respo

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 15:03

    Use the SelectMany and First/FirstOrDefault (if you are needing one value)

    List questions = //initialization;
    var someAnswer = questions.SelectMany(q=>q.Answers)
                              .First(a=>a.Name =="MyName");
    

提交回复
热议问题