Problems with internal joins in LINQ

I have a friend and I have problems with LINQ

We are trying to map a zipcode where the country name is found with a foreign key in the zipcode table.

our LINQ call is as follows

var Zipcodes = from p in db.Zipcodes
   join i in db.ISOes on p.Iso_id equals i.ID
   select new { Zip = p.Zip, name = p.Name, Address = p.Address, Country = i.Country };

return View(Zipcodes.ToList());

We can display the Zipcode table with a foreign key showing its own value, but try to use this value to display the name of the country instead (which is in the ISOes table)

We get the value cannot be null error (paraname = outer)

LINQ is a new technology for us, so any help would be greatly appreciated.

Thanks in advance!

OBS:

We redo some of the code and get stuck in what we think is another problem. we are going to make a new message about it. Sorry for the time spent, although we learned a lot :)

+3
3

, . , (null). . , , .

+2

var zipcodes = from p in db.Zipcodes
select new { Zip = p.zip, ..., country = p.ISOs.Country}

, FK

+1

Can you try this?

var Zipcodes = from p in db.Zipcodes
                             join i in db.ISOes on p.Iso_id equals i.ID
                             into ISOs
                             from iso in ISOs.DefaultIfEmpty()
                             select new { Zip = p.Zip, name = p.Name, Address = p.Address, Country = iso == null ? string.Empty : iso.Country };

        return View(Zipcodes.ToList());
0
source

All Articles