一个左外部联接如何在多个OR列上? C#和实体框架

时间:2018-06-20 21:11:20

标签: c# sql-server entity-framework linq outer-join

我目前像下面这样加入左外部联接:

join temp_locations in db.Locations 
         on new { city_id = ((int?)c.city_id), state_id = ((int?)s.state_id), country_id = ((int?)country.country_id) } 
         equals new { temp_locations.city_id, temp_locations.state_id, temp_locations.country_id } into Temp_locations
from l in Temp_locations.DefaultIfEmpty()

但是,这是使用多个AND命令创建左外部联接。我需要那些是或。 SQL示例:

Left Outer Join 
    Locations l on l.city_id = c.city_id 
                or l.state_id = s.state_id 
                or l.country_id = country.country_id

我该怎么做?

编辑18/11/11

我无法使它正常工作。交叉联接答案不起作用,因为原本不应该出现的记录正在出现或消失。我最终制作了一个存储过程,并在那里完成了所有工作。如果有人知道,我仍然愿意在实体框架中执行此操作。

1 个答案:

答案 0 :(得分:0)

交叉连接是通过两个from子句完成的。对于您而言,您没有任何等值连接条件,因此您只需要在Where上使用db.Locations来处理左外部连接:

from temp_locations in db.Locations.Where(tl =>
         c.city_id == tl.city_id ||
         s.state_id == tl.state_id ||
         country.country_id == tl.country_id)
    .DefaultIfEmpty()