左外部联接为空

时间:2019-06-18 20:58:31

标签: asp.net linq model-view-controller

我正在尝试使用LINQ进行两个Left Joins查询的并集。我需要将null单元格设置为0,但我不知道如何处理ID为null的联合的第二个拥有者,因为编译器不喜欢ID,因为ID不能为null。

我尝试了几种不同的查询联接,无联接,defaultIfEmpty,IfNotNull。
我的联接看起来像这样,但是不会编译,因为b.ID不可为空(即使当我在SQL中联接时,当ACCESS_LEVEL表中没有匹配的记录时,该字段也为null。

var resultActions = (from a in maax.CODE_TABLE.Where(a => (a.CODE_TABLE_NAME == "ACTION"))
from b in maax.ACCESS_LEVEL
.Where(b => (a.ID == b.ACTION_ID) && (functAreaId == b.FUNCT_AREA_ID) && (accessLevel == b.ACCESS_LEVEL_ID) && (b.ID > 0))                                                
.DefaultIfEmpty() 
select new UserAction()
{ 
    ACCESS_LEVEL = b.ACCESS_LEVEL_ID,
    FUNCTIONAL_AREA_ID = b.FUNCT_AREA_ID,
    ACTION_ID = a.ID,
    ACTION_NAME = a.CODE,
    isCheckeddb = true
    })
.Union(from a in maax.CODE_TABLE.Where(b => (b.CODE_TABLE_NAME == "ACTION"))
from b in maax.ACCESS_LEVEL.DefaultIfEmpty()
.Where(b => (a.ID == b.ACTION_ID) && (functAreaId == b.FUNCT_AREA_ID) && (accessLevel == b.ACCESS_LEVEL_ID) && (b.ID is null))
select new UserAction()
 {

   ACCESS_LEVEL = 0,
   FUNCTIONAL_AREA_ID = b.FUNCT_AREA_ID,
   ACTION_ID = a.ID,
   ACTION_NAME = a.CODE,
   isCheckeddb = false
 }).ToList();

我也尝试过此查询,但由于b.ACCESS_LEVEL_ID和b.FUNCT_AREA_ID为空,因此它无法运行。我曾尝试在模型中将它们设置为字符串,但这无法正常工作,因为我必须将其转换为字符串。

var resultActions = (from a in maax.CODE_TABLE.Where(a => (a.CODE_TABLE_NAME == "ACTION"))
from b in maax.ACCESS_LEVEL
.Where(b => (a.ID == b.ACTION_ID) && (functAreaId == b.FUNCT_AREA_ID) && (accessLevel == b.ACCESS_LEVEL_ID))                                                
.DefaultIfEmpty() 
select new UserAction()
{

    ACCESS_LEVEL = b.ACCESS_LEVEL_ID,
    FUNCTIONAL_AREA_ID = b.FUNCT_AREA_ID,
    ACTION_ID = a.ID,
    ACTION_NAME = a.CODE
    }).ToList();

我想获得类似的信息,因此我可以使用true / false来确定是否应选中视图中的复选框。

ID  code                   ID FUNCT_AREA_ID ACCESS_LEVEL_ID (No column name)
1004    SEARCH                  712 1000    2   true
1005    SEARCH_MINE             713 1000    2   true
1006    READ                    714 1000    2   true
1007    CREATE                  715 1000    2   true
1008    UPDATE                  716 1000    2   true
1009    DELETE                  NULL    NULL    NULL    false
1010    APPROVE                 NULL    NULL    NULL    false
1011    DENY                    NULL    NULL    NULL    false
1043    BATCH_RESEND            NULL    NULL    NULL    false

0 个答案:

没有答案