You're misreading the error, compounded by VB.NET's type coercion.
Group.FirstOrDefault(Function(x) x.VatRate).VatRate
FirstOrDefault returns the first value matching the predicate function, or the default value of T
if nothing matches. Since your function doesn't evaluate to true
or false
, VB.NET coerces the int
to a bool
. In that scenario, 0
is converted to false
and any other value is converted to true
.
So, when x.VatRate
is not 0
, then the first IndividualCharge
passes the predicate filter. When VatRate
is 0
, then no IndividualCharge
passes the predicate filter and null
(the default value of IndividualCharge
) is returned. Trying to grab the VatRate
then results in your NullReferenceException
.
Presumably, you really wanted the first VatRate
, whether it was 0 or not. Changing your code to:
Group.Select(Function(x) x.VatRate).First()
should accomplish that.