C# 使用LINQ获取一个列表中的项目<>;,在另一个列表中<&燃气轮机;
我假设有一个简单的LINQ查询来实现这一点,我只是不确定如何实现。请参阅下面的代码片段,注释解释了我要做的事情:C# 使用LINQ获取一个列表中的项目<>;,在另一个列表中<&燃气轮机;,c#,linq,C#,Linq,我假设有一个简单的LINQ查询来实现这一点,我只是不确定如何实现。请参阅下面的代码片段,注释解释了我要做的事情: class Program { static void Main(string[] args) { List<Person> peopleList1 = new List<Person>(); peopleList1.Add(new Person() { ID = 1 }); peopleList1.Add(new Person
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 });
peopleList1.Add(new Person() { ID = 4});
peopleList1.Add(new Person() { ID = 5});
List<Person> peopleList2 = new List<Person>();
peopleList2.Add(new Person() { ID = 1 });
peopleList2.Add(new Person() { ID = 4});
//I would like to perform a LINQ query to give me only
//those people in 'peopleList1' that are in 'peopleList2'
//this example should give me two people (ID = 1& ID = 4)
}
}
class Person
{
public int ID { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
List peopleList1=新列表();
添加(newperson(){ID=1});
添加(newperson(){ID=2});
添加(newperson(){ID=3});
添加(newperson(){ID=4});
添加(newperson(){ID=5});
List peopleList2=新列表();
添加(newperson(){ID=1});
添加(newperson(){ID=4});
//我想执行一个LINQ查询,只给我
//“peopleList1”中的人与“peopleList2”中的人
//这个例子应该给我两个人(ID=1&ID=4)
}
}
班主任
{
公共int ID{get;set;}
}
Product[]fruits1={新产品{Name=“apple”,code=9},
新产品{Name=“orange”,代码=4},
新产品{Name=“lemon”,代码=12};
产品[]水果2={新产品{Name=“苹果”,代码=9};
//从第一个数组中获取所有元素
//除了第二个数组中的元素。
i可数除=
结果1.除(结果2)外;
foreach(除外的var产品)
Console.WriteLine(product.Name+“”+product.Code);
/*
此代码生成以下输出:
橙色4
柠檬12
*/
var result=peopleList2.Where(p=>peopleList1.Any(p2=>p2.ID==p.ID))代码>您只需使用LINQ Intersect扩展方法即可
所以你会这样做:
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 });
peopleList1.Add(new Person() { ID = 4});
peopleList1.Add(new Person() { ID = 5});
List<Person> peopleList2 = new List<Person>();
peopleList2.Add(new Person() { ID = 1 });
peopleList2.Add(new Person() { ID = 4});
var result = peopleList1.Intersect(peopleList2);
}
}
类程序
{
静态void Main(字符串[]参数)
{
List peopleList1=新列表();
添加(newperson(){ID=1});
添加(newperson(){ID=2});
添加(newperson(){ID=3});
添加(newperson(){ID=4});
添加(newperson(){ID=5});
List peopleList2=新列表();
添加(newperson(){ID=1});
添加(newperson(){ID=4});
var result=peopleList1.Intersect(peopleList2);
}
}
只需重写Person类中的Equals方法。我认为您可以比较那里的ID。您可以使用
Where
来进行比较:
var result = peopleList1.Where(p => peopleList2.Any(p2 => p2.ID == p.ID));
您还可以使用
Intersect
(var result=peopleList1.Intersect(peopleList2);
),但这需要您实现一个额外的IEqualityComparer
或覆盖Person
的Equals
和GetHashCode
方法,使具有相同ID
的两个Person
实例被视为相等<代码>相交否则将执行引用相等。您可以使用
我将加入ID为的两个列表:
var inboth = from p1 in peopleList1
join p2 in peopleList2
on p1.ID equals p2.ID
select p1;
List<Person> joinedList = inboth.ToList();
或者您可以为Intersect
提供自定义IEqualityComparer
:
List<Person> joinedList = peopleList1.Intersect(peopleList2).ToList();
public class PersonIdComparer: IEqualityComparer<Person>
{
public bool Equals(Person x, Person y)
{
if(object.ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
return x.ID == y.ID;
}
public int GetHashCode(Person obj)
{
return obj == null ? int.MinValue : obj.ID;
}
}
公共类个人比较者:IEqualityComparer
{
公共布尔等于(人x,人y)
{
if(object.ReferenceEquals(x,y))返回true;
如果(x==null | | y==null)返回false;
返回x.ID==y.ID;
}
public int GetHashCode(Person obj)
{
返回obj==null?int.MinValue:obj.ID;
}
}
现在您可以这样使用它:
List<Person> joinedList = peopleList1
.Intersect(peopleList2, new PersonIdComparer())
.ToList();
List joinedList=peopleList1
.Intersect(人员列表2,新人员比较())
.ToList();
Enumerable.Join
和Enumerable.Intersect
都是有效的,因为它们使用的是一个集合。如果不重写Equals
+GethashCode
,,因此,目前没有太大的帮助,只有当Person有一个重写的Equals方法,该方法将两个Person实例定义为相等(如果它们的ID相等)。@Rene Vogt:Intersect
在调用Equals
之前首先使用GetHashCode
,这就是为什么Intersect
:)的完整实现链接@RenéVogt谢谢,没有完全考虑到这一点。但我在答案中添加了这一点。:)这是最好的答案。可悲的是,从选票上看,没有人关心效率和正确的做事方式。他们所需要的只是一种更短/更简单的方法。较差的软件用户:(+1)
public class PersonIdComparer: IEqualityComparer<Person>
{
public bool Equals(Person x, Person y)
{
if(object.ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
return x.ID == y.ID;
}
public int GetHashCode(Person obj)
{
return obj == null ? int.MinValue : obj.ID;
}
}
List<Person> joinedList = peopleList1
.Intersect(peopleList2, new PersonIdComparer())
.ToList();