I have the following code:
var allCountryRates = (from c in allCountryCombinations
join r in Db.PaymentRates_VisaImmigrationPermit
on new { c.HomeCountryId, c.HostCountryId }
equals new { r.HomeCountryId, r.HostCountryId }
select r);
Basically, if an r
is found in c
, based on both conditions of the join, I want to select r
. If no r
is found for c
, then I want to generate an empty record with a Guid.NewGuid()
and select that.
Is this possible? I feel like I am pretty close, but not sure how to take it further.
My code above gives me an error on the join
which reads, "The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'."
Ref. How to do joins in LINQ on multiple fields in single join
Edit: Latest version.
var allCountryRates = (from c in allCountryCombinations
join r in Db.PaymentRates_VisaImmigrationPermit
on new { home = (Guid?)c.HomeCountryId, host = (Guid?)c.HostCountryId }
equals new { home = r.HomeCountryId, host = r.HostCountryId }
into matches
from rate in matches.DefaultIfEmpty(new PaymentRates_VisaImmigrationPermit
{
Id = Guid.NewGuid(),
HomeCountryId = c.HomeCountryId,
HostCountryId = c.HostCountryId
})
select new VisaAndImmigrationPermitRate
{
Id = rate.Id,
HomeCountryId = (Guid)rate.HomeCountryId,
HomeCountry = c.HomeCountry,
HostCountryId = (Guid)rate.HostCountryId,
HostCountry = c.HostCountry
});