Accepted answer

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();


few things to fix here:

  1. no parenthesis in class declaration
  2. make the "correct" property as public
  3. and then do the selection

your code will look something like this

list<answer> answers = new list<answer>();
/* test
answers.add(new answer() { correct = false });
answers.add(new answer() { correct = true });
answers.add(new answer() { correct = false });
answer answer = answers.single(a => a.correct == true);

and the class

class answer
   public bool correct;


answers = answers.groupby(a => => x.first());

this will select each unique object by email


i think you are looking for this?

var correctanswer = answers.first(a => a.correct);

you can use single by typing :

var correctanswer = answers.single(a => a.correct);


of course!

use firstordefault() to select the first object which matches the condition:

answer answer = answers.firstordefault(a => a.correct);

otherwise use where() to select a subset of your list:

var answers = answers.where(a => a.correct);


if a.correct is a bool flag for the correct answer then you need.

answer answer = answers.single(a => a.correct);


your expression is never going to evaluate.

you are comparing a with a property of a.

a is of type answer. a.correct, i'm guessing is a boolean.

long form:-

answer = answer.singleordefault(a => a.correct == true);

short form:-

answer = answer.singleordefault(a => a.correct);


i assume you are getting an exception because of single. your list may have more than one answer marked as correct, that is why single will throw an exception use first, or firstordefault();

answer answer = answers.firstordefault(a => a.correct);

also if you want to get list of all items marked as correct you may try:

list<answer> correctedanswers =  answers.where(a => a.correct).tolist();

if your desired result is single, then the mistake you are doing in your query is comparing an item with the bool value. your comparison

a == a.correct

is wrong in the statement. your single query should be:

answer answer = answers.single(a => a.correct == true);

or shortly as:

answer answer = answers.single(a => a.correct);

Related Query

More Query from same tag