gpt4 book ai didi

sql - LINQ 右外联接在联接中有多个条件

转载 作者:行者123 更新时间:2023-12-04 04:54:19 24 4
gpt4 key购买 nike

我正在尝试将以下 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 表中存在记录。我在做 right outer连接到 ClientCommandSessionProcessed 表(在 on 中有两个条件)并检查该连接的结果是否为空 - 如果该表中有记录,则查询不应返回结果,因为这意味着它已被处理通过该 session 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 };

有谁知道是否可以向连接添加第二个条件?如果没有,是否有解决此类问题的方法?

谢谢!

最佳答案

你可以这样做:

var result = from x in table1
join y in table2
on new { x.field1, x.field2 } equals new { y.field1, y.field2 }

关于sql - LINQ 右外联接在联接中有多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16990283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com