📜  根据值 csharp 从列表中选择一个对象 - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:14.135000             🧑  作者: Mango

代码示例1
// this will return the first correct answer,
// or throw an exception if there are no correct answers
var correct = answers.First(a => a.Correct); 

// this will return the first correct answer, 
// or null if there are no correct answers
var correct = answers.FirstOrDefault(a => a.Correct); 

// this will return a list containing all answers which are correct,
// or an empty list if there are no correct answers
var allCorrect = answers.Where(a => a.Correct).ToList();