我有两个集合,这两个集合都有属性电子邮件。我需要在第一个列表中获得电子邮件不存在于第二个列表中的项目列表。对于SQL,我只会使用“not in”,但我不知道在LINQ中等价。怎么做呢?

到目前为止,我有一个连接,比如。

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

但我不能加入,因为我需要的差异和连接将失败。我需要一些方法使用包含或存在我相信。我只是还没有找到这样做的例子。


当前回答

或者你可以这样做:

var result = list1.Where(p => list2.All(x => x.Id != p.Id));

其他回答

对于任何想要在c#中使用类似sql的IN操作符的人,请下载这个包:

Mshwf。NiceLinq

它有In和NotIn方法:

var result = list1.In(x => x.Email, list2.Select(z => z.Email));

你也可以这样用

var result = list1.In(x => x.Email, "a@b.com", "b@c.com", "c@d.com");

你可以使用Where和Any的组合来查找not in:

var NotInRecord =list1.Where(p => !list2.Any(p2 => p2.Email  == p.Email));

我没有用LINQ to Entities测试这个:

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

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

另外:

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

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

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

你可以在两个不同的列表中取两个集合,比如list1和list2。

然后就写

list1.RemoveAll(Item => list2.Contains(Item));

这是可行的。

在使用ADO的情况下。NET实体框架,EchoStorm的解决方案也很完美。但我花了几分钟才明白。假设你有一个数据库上下文dc,并且想要在表x中找到没有在表y中链接的行,完整的答案如下所示:

var linked =
  from x in dc.X
  from y in dc.Y
  where x.MyProperty == y.MyProperty
  select x;
var notLinked =
  dc.X.Except(linked);

针对Andy的评论,是的,在LINQ查询中可以有两个from。下面是一个使用列表的完整工作示例。每个类Foo和Bar都有一个Id。Foo通过Foo. barid对Bar有一个“外键”引用。程序选择所有未链接到相应Bar的Foo。

class Program
{
    static void Main(string[] args)
    {
        // Creates some foos
        List<Foo> fooList = new List<Foo>();
        fooList.Add(new Foo { Id = 1, BarId = 11 });
        fooList.Add(new Foo { Id = 2, BarId = 12 });
        fooList.Add(new Foo { Id = 3, BarId = 13 });
        fooList.Add(new Foo { Id = 4, BarId = 14 });
        fooList.Add(new Foo { Id = 5, BarId = -1 });
        fooList.Add(new Foo { Id = 6, BarId = -1 });
        fooList.Add(new Foo { Id = 7, BarId = -1 });

        // Create some bars
        List<Bar> barList = new List<Bar>();
        barList.Add(new Bar { Id = 11 });
        barList.Add(new Bar { Id = 12 });
        barList.Add(new Bar { Id = 13 });
        barList.Add(new Bar { Id = 14 });
        barList.Add(new Bar { Id = 15 });
        barList.Add(new Bar { Id = 16 });
        barList.Add(new Bar { Id = 17 });

        var linked = from foo in fooList
                     from bar in barList
                     where foo.BarId == bar.Id
                     select foo;
        var notLinked = fooList.Except(linked);
        foreach (Foo item in notLinked)
        {
            Console.WriteLine(
                String.Format(
                "Foo.Id: {0} | Bar.Id: {1}",
                item.Id, item.BarId));
        }
        Console.WriteLine("Any key to continue...");
        Console.ReadKey();
    }
}

class Foo
{
    public int Id { get; set; }
    public int BarId { get; set; }
}

class Bar
{
    public int Id { get; set; }
}