【问题标题】:linq equivalent sql query "not in (select query)"linq 等效 sql 查询“不在(选择查询)”
【发布时间】:2012-12-22 09:15:32
【问题描述】:

我的sql代码如下:

select UserId,UserName 
from aspnet_Users 
where UserId not in (select UsersId from tbluser  where active='true')

什么是等价的 linq 表达式?

【问题讨论】:

  • !.Contains() 或 .Except() 是我知道的两种方式

标签: .net sql linq


【解决方案1】:

我第一次尝试在C# 中使用LiNQ

var result = from y in aspnet_Users
            where !(
                        from x in tblUser
                        where  x.active == "true"
                        select x.UsersID
                    ).Contains(y.UserId)
            select y;                
            -- OR // select new { y.UserId, y.UserName};

来源

【讨论】:

  • 我觉得应该是x.active
  • 链接失效了,我在webarchive here找到了最后一个版本
【解决方案2】:
var query =
    from c in aspnet_Users 
    where !(from o in tbluser where o.active=="true" 
            select o.UserId)
           .Contains(c.UserId)
    select c;

【讨论】:

  • 您的 copy 与 @JW 的答案有何不同?
  • 我没有观察@jw 的答案,发布我的答案后我才知道两者都是一样的。
  • where !aspnet_Users.Any(p => p.active== "true") 看起来不一样。我在这里错过了什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 2021-12-18
相关资源
最近更新 更多