首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在连接中具有多个条件的LINQ右外连接

在连接中具有多个条件的LINQ右外连接
EN

Stack Overflow用户
提问于 2013-06-08 02:02:50
回答 1查看 1.4K关注 0票数 2

我正在尝试将以下SQL转换为LINQ:

代码语言:javascript
复制
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的了解:

代码语言:javascript
复制
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 };

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

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-08 02:13:51

你可以这样做:

代码语言:javascript
复制
var result = from x in table1
         join y in table2
         on new { x.field1, x.field2 } equals new { y.field1, y.field2 }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16990283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档