简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM