0

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

Gilad Green
  • 35,761
  • 7
  • 54
  • 89
Karl O'Connor
  • 1,245
  • 2
  • 9
  • 9
  • It seems fine to me but try to log the generated SQL by using this _context.Database.Log = s => Debug.WriteLine(s). Also check the relation between your classes – alnaji Jul 13 '16 at 09:27

3 Answers3

2

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

Community
  • 1
  • 1
ilkerkaran
  • 3,864
  • 3
  • 24
  • 39
1
var ins = 
    _context.Institution
    .Join(_context.Country, 
          inst => inst.country_code,        
          ctry => ctry.iso3,   
         (inst, ctry) => new { Institution = inst, Country = ctry }).ToList(); 
Ciro Corvino
  • 1,818
  • 5
  • 17
  • 31
0

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

Karl O'Connor
  • 1,245
  • 2
  • 9
  • 9