1
下,我有以下代碼加入兩個列表條件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestSomethingRelatedToLINQ
{
class Person : IEqualityComparer<Person>
{
internal int personID;
internal string PersonName;
public bool Equals(Person x, Person y)
{
return x.personID == y.personID;
}
public int GetHashCode(Person obj)
{
return obj.personID.GetHashCode();
}
}
class Program
{
static void Main(string[] args)
{
List<Person> list1 = new List<Person>{
new Person{personID = 1 , PersonName = "Ali"},
new Person{personID = 2 , PersonName = "Ali1"},
new Person{personID = 3 , PersonName = "Ali2"}
};
List<Person> list2 = new List<Person>{
new Person{personID = 4 , PersonName = "Habib1"},
new Person{personID = 2 , PersonName = "Habib2"},
new Person{personID = 5 , PersonName = "Habib3"}
};
}
}
}
,我需要選擇列表1中的所有元素,並在不具備PERSONID存在於列表1 list2中的所有元素,然後責令結果由PERSONID
新的輸出應該像
1, Ali
2, Ali1
3, Ali2
4, Habib1
5, Habib3
任何想法如何使用
非常感謝這個建議,我在修理它後非常好用 – AMH 2012-04-08 13:14:19