score:73

Accepted answer

if your collection is empty, firstordefault will return default(optionalextras). the default value of a struct is the struct with all its values in turn default initialized (i.e. zero, null, etc.).

if you assume that there will be an element and your code doesn't work with an empty collection, use first() instead, since that will throw an exception when your collection is empty. it's generally better to fail fast than to return wrong data.

if you cannot assume that there will be an element, but also cannot deal with struct default initialization, you might make the structs in the collection a nullable value type, for example as follows:

optionalextras
    .where(w => w.code == optextra.code)
    .cast<optionalextra?>()
    .firstordefault();

this way you can get a null return even for a struct. the key idea here is to extend the set of possible values to include something other than an optionalextra to allow detection of an empty list. if you don't like nullables, you could instead use a maybe<> implementation (not a .net builtin), or use an empty-or-singleton list (e.g. .take(1).toarray(). however, a nullable struct is likely your best bet.

tl;dr;

  • .firstordefault<t>() returns default(t) if the sequence is empty
  • use .first() instead if you assume the list is non-empty.
  • cast to nullable and then use .firstordefault<t>() when you cannot assume the list is non-empty.

score:-1

assuming code is a string for the purposes of my answer, you should be able just to test that value for its default.

optionalextra multioptextra = optionalextras.where(w => w.code == optextra.code).firstordefault();
if (multioptextra.code != null)
{

}

score:0

if you want to check for null, use system.nullable collection:

var optionalextras = new list<optionalextra?>();
/* add some values */
var extras = optionalextras.firstordefault(oe => oe.value.code == "code");
if (extras != null)
{
    console.writeline(extras.value.code);
}

note that you have to use value to access the element.

score:2

its provide you defualt value for your structure like as below

int[] numbers = { };
int first = numbers.firstordefault();
console.writeline(first);//this print 0 as output 

other option to handle is make use of default value like as below

list<int> months = new list<int> { };
// setting the default value to 1 by using defaultifempty() in the query. 
int firstmonth2 = months.defaultifempty(1).first();
console.writeline("the value of the firstmonth2 variable is {0}", firstmonth2);

score:3

the result will be the default value of your struct, e.g. default(optionalextras).

whereas for a reference type the default value is null.

score:4

if default(optionextra) is still a valid value, it's better to change your code to this

var results = optionalextras.where(w => w.code == optextra.code).take(1).tolist();
if (results.any()) {
   multioptextra = results[0]
}

score:7

as others have said, the result of your code when no elements match will be:

default( optionalextra )

if you want a null returned, you can cast your list to optionalextra?

optionalextra? multioptextra = optionalextras.cast<optionalextra?>().where( ...

you can then test for null


Related Query

More Query from same tag