score:63
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();
score:0
few things to fix here:
- no parenthesis in class declaration
- make the "correct" property as public
- 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;
}
score:0
answers = answers.groupby(a => a.id).select(x => x.first());
this will select each unique object by email
score:1
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);
score:1
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);
score:2
if a.correct
is a bool
flag for the correct answer then you need.
answer answer = answers.single(a => a.correct);
score:5
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);
score:16
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);
Source: stackoverflow.com
Related Query
- Linq select object from list depending on objects attribute
- Return a specific object in a LINQ select query from two different objects in a list
- LINQ select one field from list of DTO objects to array
- How to use Linq to select and group complex child object from a parents list
- Linq to select list of items from custom nested list of objects
- LINQ sample: select typed objects from a list
- Remove an object from a list based on an attribute of another list with linq
- What is the correct way of retrieving a given business object from a list of business objects using LINQ Where vs. Find?
- Select objects from a list using LINQ based on a property
- c# - Linq select properties from List of objects and modify them with shorthand conditional
- Linq select Objects from List which have empty String
- Using LINQ to isolate a particular object from a list of objects
- Linq code to get the index of an object in an array from an object within a list
- Generate a list of custom objects from Json object using LINQ
- Using a Linq query to select objects where any value of a property matches values from a list
- Select values from multiple dictionaries into list of objects with LINQ
- How to use LINQ to get an object from List of objects matching option from List of options
- Return select objects and only desired subvalues from a list of lists using LINQ
- c# Linq or code to extract groups from a single list of source data
- Creating sub lists with objects of the same attribute from a list using LINQ
- Linq select objects in list where exists IN (A,B,C)
- Create a list from two object lists with linq
- Select Multiple Fields from List in Linq
- LINQ query to return distinct field values from list of objects
- LINQ: Select where object does not contain items from list
- How to select values within a provided index range from a List using LINQ
- Create a list of one object type from a list of another using Linq
- C# LINQ select from list
- Linq to Select Parent Objects Where Child Objects Have a Matching Child Object
- C# LINQ select from where value is not contained in array / list
More Query from same tag
- Type interference in the call join
- How to take X amount of questions, that sum up Y amount of difficulty
- Simple Query in LINQ
- To find the best Projects depending on the Project Likes, Project Connected and Project Rating
- Visiting IEnumerable<T> children
- Handling null in Linq
- LINQ conversion issue
- Extract nested object structure from flat list
- In clause in Linq to SQL
- MVC Core 2 - && operator in lambda expression not working
- LINQ advice on selecting distinct elements from a collection
- Can I use a CAST inside a LINQ to Entities query?
- Reading XML using XDocument & Linq - check if element is NULL?
- Entity Framework Linq foreach performance vs using Select() instead
- Linq to SQL - Find element with lowest field value - Performance
- filtering a collection using Linq - how to reuse the same filter
- Need help with a linq query please!
- MVC orderby on dropdownlist
- C# populating class array from xml linq results in FormatException
- LINQ Lazy load or query incorrect
- LINQ Group and Count with a Max
- No idea why my MVC Entity-Framework app cannot find elements in database
- LINQ Expression Conversion / Concat from Int to string
- using LINQ how can i concatenate string properties from itesm in a collection
- Get distinct value from comma seperated array using LINQ
- linq/lambda best approach to project anonymous type to strong type
- Linq query with month difference between 2 dates
- Why are parenthesis needed in F# using method chaining query expression where clause?
- Can't reference my Entity Model for Sql-to-Linq query
- DistinctBy but ignore null/empty