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