Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8816699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:40:43+00:00 2026-06-14T04:40:43+00:00

I have a list of objects class Answer { bool correct; } List<Answer> Answers

  • 0

I have a list of objects

class Answer
{
   bool correct;
}

List<Answer> Answers = new List<Answer>();

Is there a way in linq for me to select an object depending on its attribute?

So far I have

Answer answer = Answers.Single(a => a == a.Correct);

But it does not work

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:40:44+00:00Added an answer on June 14, 2026 at 4:40 am

    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 can also consider using 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), or Where followed by ToList (which will return all elements which satisfy the criteria):

    // this will either find a single correct answer,
    // or throw an exception if there are no correct answers
    // or if there is more than one correct answer
    var correct = answers.Single(a => a.Correct);
    
    // this will either find a single correct answer,
    // or return null if there are no correct answers,
    // or throw an exception if there is more than one correct answer
    var correct = answers.SingleOrDefault(a => a.Correct);
    
    // 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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Player that contains a list of Accessory objects. There are
I have a Question class that contains a list of Answer objects. In my
I have a list of objects called Activity: class Activity { public Date activityDate;
If I have a list of objects called Car: public class Car { public
I have a Ticket class that contains a List of User objects: class Ticket
I have a stl::list containing Widget class objects. They need to be sorted according
I have objects of a class Choice which are in a list. Choice looks
I have class A and list of A objects. A has a function f
I have a class that holds another objects inside it (List, Set and objects
I have a class that has an list<Book> in it, and those Book objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.