左联接在具有多个联接的查询中失败

本文关键字:查询 失败 | 更新日期: 2023-09-27 18:36:07

在它被称为重复项之前,我已经在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)

我希望这有所帮助。

左联接在具有多个联接的查询中失败

试试这个答案中的语法:https://stackoverflow.com/a/4739738/1869660
我认为在创建左连接时,这是一种更简单的语法(注意: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 ...