Reputation: 21430
My query is as follows:
(from c in countries
join r in Db.PaymentRates_VisaImmigrationPermit on c.CountryId equals r.HomeCountryId into countryRates
from legalRate in countryRates.DefaultIfEmpty()
join hostC in Db.Countries on legalRate.HostCountryId equals hostC.Id
select new [...]
I get a null reference exception on this line:
join hostC in Db.Countries on legalRate.HostCountryId equals hostC.Id
... Which is obviously caused by this line:
from legalRate in countryRates.DefaultIfEmpty()
How can I do the join only if the legalRate
isn't null; so as to get the data I want without incurring a null reference exception?
Similar question: Error in LINQ Left JOIN
Upvotes: 2
Views: 139
Reputation: 101681
You can set the default value of your legalRate
using the DefaultIfEmpty
constructor:
from legalRate in
countryRates.DefaultIfEmpty(new CountryRate { HostCountryId = int.MaxValue })
Upvotes: 1