我正在尝试将以下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表中存在记录。我正在对ClientCommandSessionProcessed表执行right outer连接(在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 };有没有人知道有没有可能给连接添加第二个条件?如果没有,是否有解决此类问题的方法?
谢谢!
发布于 2013-06-08 02:13:51
你可以这样做:
var result = from x in table1
join y in table2
on new { x.field1, x.field2 } equals new { y.field1, y.field2 }https://stackoverflow.com/questions/16990283
复制相似问题