I have the following code
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"}
};
}
}
}
I need to select all the elements in list1 , and all the element on list2 that doesn’t have personID exists in List1 , then order the result by the personID
the new output should be like
1, Ali
2, Ali1
3, Ali2
4, Habib1
5, Habib3
any idea how to use that
First call LINQ Concat, such that you append List 2 to List 1.
Then call LINQ Distinct, which will remove the entries that are duplicate (either the former or the latter).
This site suggest classes you are used, you just implement the IEqualityComparer interface.