Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, May 17, 2013 9:43 AM
Hi,
I have two lists of people i.e. List<Person>.
I want to have a final list that will have each unique person from both list A and list B.
A person who is in List A may or may not be in List B and vice versa. Therefore, the final list List C should have all the unique persons from both lists.
How can I construct this LINQ statement?
P.S. To keep things simple, the Person class has three properties i.e. PersonId, FirstName and LastName
All replies (5)
Friday, May 17, 2013 9:58 AM ✅Answered
You use the Union operator
List<Person> persons1 = new List<Person>() { p1, p2, p3};
List<Person> persons2 = new List<Person>() {p3, p4 };
List<Person> uniquePersons = persons1.Union(persons2).ToList();
uniquePersons will have p1, p2, p3, & p4
Friday, May 17, 2013 10:03 AM ✅Answered
Hi,
Check this code
List<String> list1 = new List<String>() { "A", "B", "C", "D", "E" };
List<String> list2 = new List<String>() { "B", "C", "D" };
List<String> list3 = new List<String>() { "A", "B", "C" };
var commonList = list1.Union(list2).Union(list3).Distinct();
foreach (var item in commonList)
{
Console.WriteLine(item);
}
OUTPUT:
A
B
C
D
E
Hope it helps you.
Cheers
Friday, May 17, 2013 10:10 AM
Thank you!
Friday, May 17, 2013 10:15 AM
You don't need to use Distinct with the Union operator. When you Union 2 lists, you are taking only the unique values from each.
Friday, May 17, 2013 10:18 AM
Try
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
var listA = new List<Person>()
{
new Person(){PersonId = 1, FirstName = "Nasser", LastName="Malik" },
new Person(){PersonId = 2, FirstName = "John", LastName="Cina" },
new Person(){PersonId = 1, FirstName = "Nasser", LastName="Malik" },
new Person(){PersonId = 3, FirstName = "Sam", LastName="Uan" }
};
var listB = new List<Person>()
{
new Person(){PersonId = 4, FirstName = "Bill", LastName="Gates" },
new Person(){PersonId = 1, FirstName = "Nasser", LastName="Malik" },
new Person(){PersonId = 5, FirstName = "Phill", LastName="Jacks" }
};
//Get unique records if ListA has duplicate records
var listC = listA.GroupBy(i => i.PersonId).Select(g => g.First()).ToList();
//Add ListB records to ListC
foreach (var person in listB)
{
//Check if record already exists in ListC
if (listC.Where(a => a.PersonId == person.PersonId).Count() == 0)
{
listC.Add(person);
}
}
foreach(var person in listC)
{
Response.Write("PersonId = " + person.PersonId + " | FirstName = " + person.FirstName + " | LastName = " + person.LastName + "</br>");
}
}
Output:
PersonId = 1 | FirstName = Nasser | LastName = Malik
PersonId = 2 | FirstName = John | LastName = Cina
PersonId = 3 | FirstName = Sam | LastName = Uan
PersonId = 4 | FirstName = Bill | LastName = Gates
PersonId = 5 | FirstName = Phill | LastName = Jacks
Regards.