Karl O'Connor
Karl O'Connor

Reputation: 1565

Entity Framework linq join/association

I'm trying to do a join/ association in entity framework using linq.

I have the following two tables:

Institution

id name country_code(same as iso3) etc...

Country

id name iso3

I have tried the following but i'm getting stuck at where to associate the objects with each other:

List<Institution> ins = _context.Institution
                                .Include(o => o.country)

thanks

Upvotes: 0

Views: 311

Answers (3)

Ciro Corvino
Ciro Corvino

Reputation: 2128

var ins = 
    _context.Institution
    .Join(_context.Country, 
          inst => inst.country_code,        
          ctry => ctry.iso3,   
         (inst, ctry) => new { Institution = inst, Country = ctry }).ToList(); 

Upvotes: 1

Karl O&#39;Connor
Karl O&#39;Connor

Reputation: 1565

Thanks for your help, I've added an extra column to the institution table called country_id, i'm sure there's probably a way I could have mapped the country_code of country to the institution entity, but I took the quick and easy way out.

Thanks again

Upvotes: 0

ilkerkaran
ilkerkaran

Reputation: 4344

if iso3 and country_code is not bound with a foreign constraint you should use .Join for lambda expression. Check the link here

Upvotes: 2

Related Questions