Check if single() LINQ return NULL
Use SingleOrDefault
instead.
Single
throws an exception when the enumeration does not contain exactly one element, SingleOrDefault<T>
returns default(T)
(which is null
for reference types) when called on empty enumerations instead. Note that both will throw if there is more than one element in the enumeration.
By design, Single
will throw an exception when the sequence is empty. Use SingleOrDefault
to return null
when your sequence is empty.