First, Single
throws an exception if there is more than one element satisfying the criteria. Second, your criteria should only check if the Correct
property is true
. Right now, you are checking if a
is equal to a.Correct
(which will not even compile).
You should use First
(which will throw if there are no such elements), or FirstOrDefault
(which will return null
for a reference type if there isn't such element):
// 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();