After reading the example of Microsoft here
var query = from Student student in arrList
where student.Scores[0] > 95
select student;
foreach (Student s in query)
Console.WriteLine(s.LastName + ": " + s.Scores[0]);
I have a question :
Is it possible to avoid the loop ?
foreach (Student s in query)
Console.WriteLine(s.LastName + ": " + s.Scores[0]);
What if i rewrite the query and assume my query will always return one result.
var query = from Student student in arrList
where student.FirstName == "Cesar"
select student;
I dont need to do a loop… I know that my query return only one element.
It will still be an
IEnumerable, but with one item. If you want to extract it you can use First() or even better Single():Also you can replace the entire
whereclause by usingSingle/Firstoverloaded methods:Singleis more strict thanFirstsince it will also check that there is exactly one item returned.