我假设有一个简单的LINQ查询来做到这一点,我只是不太确定如何。
给定这段代码:
class Program
{
static void Main(string[] args)
{
List<Person> peopleList1 = new List<Person>();
peopleList1.Add(new Person() { ID = 1 });
peopleList1.Add(new Person() { ID = 2 });
peopleList1.Add(new Person() { ID = 3 });
List<Person> peopleList2 = new List<Person>();
peopleList2.Add(new Person() { ID = 1 });
peopleList2.Add(new Person() { ID = 2 });
peopleList2.Add(new Person() { ID = 3 });
peopleList2.Add(new Person() { ID = 4 });
peopleList2.Add(new Person() { ID = 5 });
}
}
class Person
{
public int ID { get; set; }
}
我想执行一个LINQ查询,给我所有人在peopleList2不在peopleList1。
这个例子应该给我两个人(ID = 4 & ID = 5)
有点晚了,但一个很好的解决方案,也是Linq到SQL兼容的是:
List<string> list1 = new List<string>() { "1", "2", "3" };
List<string> list2 = new List<string>() { "2", "4" };
List<string> inList1ButNotList2 = (from o in list1
join p in list2 on o equals p into t
from od in t.DefaultIfEmpty()
where od == null
select o).ToList<string>();
List<string> inList2ButNotList1 = (from o in list2
join p in list1 on o equals p into t
from od in t.DefaultIfEmpty()
where od == null
select o).ToList<string>();
List<string> inBoth = (from o in list1
join p in list2 on o equals p into t
from od in t.DefaultIfEmpty()
where od != null
select od).ToList<string>();
向http://www.dotnet-tricks.com/Tutorial/linq/UXPF181012-SQL-Joins-with-C致敬
这可以用下面的LINQ表达式来解决:
var result = peopleList2.Where(p => !peopleList1.Any(p2 => p2.ID == p.ID));
通过LINQ表达的另一种方式,一些开发人员认为更易于阅读:
var result = peopleList2.Where(p => peopleList1.All(p2 => p2.ID != p.ID));
警告:正如评论中所指出的,这些方法要求O(n*m)操作。这可能没问题,但可能会带来性能问题,特别是在数据集相当大的情况下。如果这不能满足您的性能要求,您可能需要评估其他选项。但是,由于声明的需求是LINQ中的解决方案,所以这里不讨论这些选项。与往常一样,根据项目的性能需求评估任何方法。