我正在尝试将以下SQL转换为LINQ:
select * from ClientCommands as cc
join ClientCommandTypes as cct on cc.ClientCommandTypeID = cct.ClientCommandTypeID
right outer join ClientCommandSessionProcessed as ccsp
-- This next line is the one I'm having trouble with:
on cc.ClientCommandID = ccsp.ClientCommandID and cc.ClientSessionID = @ClientSessionID
where
ccsp.ClientCommandSessionProcessedID is null and
cc.StartDate < getdate() and
cc.DeactivatedDate > getdate() and
(cc.ClientAppID is null or cc.ClientAppID == @ClientAppID)
基本上它正在做的是从数据库中获取ClientCommands表中的数据,除非ClientCommandSessionProcessed表中存在记录。我正在{Client}执行right outer
连接到ClientCommandSessionProcessed表(on
中有两个条件)并检查该连接的结果是否为null - 如果该表中有记录,则查询不应该不返回结果,因为这意味着它已被该会话ID处理。
我几乎在LINQ中完成了,我唯一的问题是,我似乎无法在right outer join
中使用多个条件,而且如果我坚持第二个,我认为这不会正常工作where
子句中的条件。
这是我到目前为止对LINQ的所作所为:
var clientCommands = from cc in db.ClientCommands
join cct in db.ClientCommandTypes on cc.ClientCommandTypeID equals cct.ClientCommandTypeID
join ccsp in db.ClientCommandSessionProcesseds
// How do I add a second condition here?
on cc.ClientCommandID equals ccsp.ClientCommandID into ccspR
from ccspRR in ccspR.DefaultIfEmpty()
where
ccspRR == null &&
cc.StartDate < DateTime.Now &&
cc.DeactivatedDate > DateTime.Now &&
(cc.ClientAppID == null || cc.ClientAppID == clientApp.ClientAppId)
select new { cc, cct };
有谁知道是否可以为连接添加第二个条件?如果没有,是否有解决这类问题的方法?
谢谢!
答案 0 :(得分:3)
您可以这样做:
var result = from x in table1
join y in table2
on new { x.field1, x.field2 } equals new { y.field1, y.field2 }