在Linq查询中加入null值启用

时间:2016-06-24 10:00:27

标签: c# linq

我正在运行一个Linq查询,我期望为ParentName = functionNavigation_b.Title为null,其中ParentID = functionHierarchy.Parent_Function_ID有0值,我想在列表中添加,但显然我的连接是无效的。我需要补充一点。

from functionNavigation in Sys_Nav_Functions
 join functionHierarchy in Sys_Nav_FunctionHierarchies on functionNavigation.Function_ID equals functionHierarchy.Function_ID
 join functionNavigation_b in Sys_Nav_Functions on functionHierarchy.Parent_Function_ID equals functionNavigation_b.Function_ID  
 join functionInController in Sys_Nav_FunctionInControllers on functionNavigation.Function_ID equals functionInController.Function_ID
 join functionController in Sys_Nav_FunctionControllers on functionInController.ControllerID equals functionController.ControllerID
 join functionInAction in Sys_Nav_FunctionInActions on functionNavigation.Function_ID equals functionInAction.Function_ID
 join functionAction in Sys_Nav_FunctionActions on functionInAction.ActionID equals functionAction.ActionID
    select new {
        functionNavigation.Function_ID, 
        functionNavigation.Title, 
        functionNavigation.Hierarchy_Level, 
        ParentID = functionHierarchy.Parent_Function_ID, 
        ParentName = functionNavigation_b.Title,
        functionController.ControllerID, 
        functionController.ControllerName, 
        functionAction.ActionID, 
        functionAction.ActionName }

2 个答案:

答案 0 :(得分:2)

使用Left Join获取记录

答案 1 :(得分:2)

 from functionNavigation in Sys_Nav_Functions
 join functionHierarchy in Sys_Nav_FunctionHierarchies on functionNavigation.Function_ID equals functionHierarchy.Function_ID
 join functionNavigation_b in Sys_Nav_Functions on functionHierarchy.Parent_Function_ID equals functionNavigation_b.Function_ID  
        into tmp from functionNavigation_b in tmp.DefaultIfEmpty()
 join functionInController in Sys_Nav_FunctionInControllers on functionNavigation.Function_ID equals functionInController.Function_ID
 join functionController in Sys_Nav_FunctionControllers on functionInController.ControllerID equals functionController.ControllerID
 join functionInAction in Sys_Nav_FunctionInActions on functionNavigation.Function_ID equals functionInAction.Function_ID
 join functionAction in Sys_Nav_FunctionActions on functionInAction.ActionID equals functionAction.ActionID
    select new {
        functionNavigation.Function_ID, 
        functionNavigation.Title, 
        functionNavigation.Hierarchy_Level, 
        ParentID = functionHierarchy.Parent_Function_ID, 
        ParentName = (functionNavigation_b != null) : functionNavigation_b.Title ? "", // 
        functionController.ControllerID, 
        functionController.ControllerName, 
        functionAction.ActionID, 
        functionAction.ActionName }