简体   繁体   English

实体框架linq联接/关联

[英]Entity Framework linq join/association

I'm trying to do a join/ association in entity framework using linq. 我正在尝试使用linq在实体框架中进行联接/关联。

I have the following two tables: 我有以下两个表:

Institution 机构

id name country_code(same as iso3) etc... id名称country_code(与iso3相同)等...

Country 国家

id name iso3 id名称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. 如果iso3和country_code没有绑定外部约束,则应使用.Join进行lambda表达式。 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. 感谢您的帮助,我在机构表中添加了一个名为country_id的额外列,我敢肯定我可以将country_code映射到机构实体,但是我采取了快速简便的方法。

Thanks again 再次感谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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