你会如何做一个“不在"?用 LINQ 查询? [英] How would you do a "not in" query with LINQ?

查看:24
本文介绍了你会如何做一个“不在"?用 LINQ 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个集合,它们在两个集合中都有属性 Email.我需要获取第一个列表中的项目列表,其中 Email 在第二个列表中不存在.对于 SQL,我只会使用not in",但我不知道 LINQ 中的等效项.这是怎么做的?

I have two collections which have property Email in both collections. I need to get a list of the items in the first list where Email does not exist in the second list. With SQL I would just use "not in", but I do not know the equivalent in LINQ. How is that done?

到目前为止我有一个加入,比如...

So far I have a join, like...

var matches = from item1 in list1
join item2 in list2 on item1.Email equals item2.Email
select new { Email = list1.Email };

但是我无法加入,因为我需要差异并且加入会失败.我需要某种方式来使用我相信的包含"或存在".我只是还没有找到一个例子来做到这一点.

But I cannot join since I need the difference and the join would fail. I need some way of using Contains or Exists I believe. I just have not found an example to do that yet.

推荐答案

我不知道这是否对您有帮助,但是..

I don't know if this will help you but..

NorthwindDataContext dc = new NorthwindDataContext();    
dc.Log = Console.Out;

var query =    
    from c in dc.Customers    
    where !(from o in dc.Orders    
            select o.CustomerID)    
           .Contains(c.CustomerID)    
    select c;

foreach (var c in query) Console.WriteLine( c );

来自 LINQ to SQL 中的 NOT IN 子句 by 马可·鲁索

这篇关于你会如何做一个“不在"?用 LINQ 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆