左连接在多个连接的查询中失败

时间:2015-01-05 07:34:57

标签: c# linq left-join

在此之前被称为副本之前,我已经查看了SO并找到了一种方法来执行此操作,但它无法正常工作。

我的查询是

 var GetAllProjects = from f in dc.vw_gmi_all_projects
 join mc in dc.gmi_maintenance_classes on f.maintenance_classID equals mc.maintenance_classID
 join ms in dc.gmi_maintenance_subclasses on f.maintenance_subclassID equals ms.maintenance_subclassID
 join pm in dc.master_project_milestones on f.pmID equals pm.pmID
 join ac in dc.vw_master_Countries on f.country_display_name equals ac.country_display_name
 join pd in dc.gmi_project_details on f.project_dataID equals pd.project_dataID
 join md in dc.vw2_master_districts on f.country_display_name equals md.element_display_name
 join ml in dc.vw2_master_lmus on pd.dataID equals ml.elementID into gl from sub in gl.DefaultIfEmpty()
 where (mc.maintenance_classID == 3 && ms.maintenance_subclassID != 11)
 select new
 {
     f.project_dataID,
     f.projectID,
     f.project_title,
     f.local_projectID,
     f.pm_display_name,
     f.reu_name,
     f.reuID,
     f.sectorID,
     f.sector_display_name,
     f.country_display_name,
     f.maintenance_classID,
     f.maintenance_subclassID,
     mc.maintenance_class_display_name,
     ms.maintenance_subclass_display_name,
     pm.pm_name,
     ac.region_display_name,
     pd.dataID,
     md.element_display_name,
     ac.cluster_display_name,
     display_name = sub.element_display_name
 };

foreach (var a in GetAllProjects)
{
lst.Add(new ReportFilter
{
    project_dataID = (int)a.project_dataID,
    projectID = a.projectID,
    project_title = a.project_title,
    local_projectID = a.local_projectID,
    pm_display_name = a.pm_display_name,
    reu_name = a.reu_name,
    reuID = a.reuID,
    country_display_name = a.country_display_name,
    sectorID = a.sectorID,
    sector_display_name = a.sector_display_name,
    maintenance_classID = a.maintenance_classID,
    maintenance_subclassID = a.maintenance_subclassID,
    maintenance_class_display_name = a.maintenance_class_display_name,
    maintenance_subclass_display = a.maintenance_subclass_display_name,
    pm_name = a.pm_name,
    region_display_name = a.region_display_name,
    dataID = a.dataID,
    district = a.element_display_name,
    cluster_display_name = a.cluster_display_name
});
}

这是左连接应该发生的地方..

 join ml in dc.vw2_master_lmus on pd.dataID equals ml.elementID into gl from sub in gl.DefaultIfEmpty()

如果我在左连接中省略尝试并完全离开该连接,则此查询运行正常,但我需要让左连接工作,以便我可以获取其余记录。典型的加入不会像其他人一样工作,因为它不会返回任何记录。

那么我的查询出错了,还是做错了?我知道它出了什么问题,不知道如何解决它。

由于

更多细节 此查询创建一个列表,然后我查询此列表。但是,由于我尝试创建左连接,此查询不会返回任何记录。

修改 这是我编写的SQL,并尝试使用Linq

重新创建它
select * from [vw_gmi_all_projects] f
inner join [gmi_maintenance_classes] mc on f.maintenance_classID = mc.maintenance_classID
inner join [gmi_maintenance_subclasses] ms on f.maintenance_subclassID =   ms.maintenance_subclassID
inner join [master_project_milestones] pm on f.pmID = pm.pmID
inner join [vw_master_Countries] ac on f.country_display_name = ac.country_display_name
inner join [gmi_project_details] pd on f.project_dataID = pd.project_dataID
inner join [vw2_master_district] md on f.country_display_name = md.element_display_name
left join [vw2_master_lmu] ml on pd.dataID = ml.elementID 
where (mc.maintenance_classID = 3 and ms.maintenance_subclassID != 11)

我希望这会有所帮助。

1 个答案:

答案 0 :(得分:0)

请尝试使用此答案的语法:https://stackoverflow.com/a/4739738/1869660
在创建LEFT JOIN时,我认为这是一种更简单的语法(注意:from,而不是join)。

var GetAllProjects = from f in dc.vw_gmi_all_projects
        join mc in dc...
        join ms in dc...
        join pm in dc...
        join ac in dc...
        join pd in dc...
        join md in dc...

        from ml in dc.vw2_master_lmus.Where(ml => ml.elementID == pd.dataID).DefaultIfEmpty()

        where ...