Understanding .Single ()

[This is very difficult for Google.]

Looking through the sample code, I see a usage that I don't understand:

var orderRow = order.Rows.Single();

The strings are enumerable, and they will usually be iterated using the foreach loop. Is the only one. A workaround for cases where for some reason foreach cannot (or is not necessary) be used?

THX

In the time since OP, I learned a little more about a usage that will help other hits in this matter:

var option = options.OfType<AdditionalLocationsOption>().SingleOrDefault();
if (option != null){
...stuff
}
+3
source share
2 answers

The LINQ Singlemethod returns one item in the collection.

This will be written in Rows, as you know, contains exactly one line.
If it is empty or has more than one row, an exception will be thrown.

, , , foreach .

+7

. : . , ( , - , ). . (x = > x.ID == myID).Single() .

SinglgeOrDefault ... , 1 , , 0 . null

+1

Source: https://habr.com/ru/post/1786558/