实体框架 linq join/association

本文关键字:association join linq 框架 实体 | 更新日期: 2023-09-27 17:56:47

我正在尝试使用 linq 在实体框架中进行连接/关联。

我有以下两个表:

机构

编号名字country_code(与ISO3相同)等。。。

国家

编号名字ISO3

我已经尝试了以下方法,但我卡在将对象相互关联的位置上:

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

谢谢

实体框架 linq join/association

如果 iso3 和 country_code 不受外部约束,则应使用 。联接 lambda 表达式。在此处查看链接

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

感谢您的帮助,我在机构表中添加了一个名为 country_id 的额外列,我相信可能有一种方法可以将国家country_code映射到机构实体,但我采取了快速简便的方法。

再次感谢