Find Common In Set

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
	public static void Main()
	{
		var setOfPersons = new List<List<Person>>();

		var firstPersonList=new List<Person>();
		Person p1=new Person(100,"James");
		Person p2=new Person(200,"Smith");
		firstPersonList.Add(p1);
		firstPersonList.Add(p2);
		
		setOfPersons.Add(firstPersonList);
		
		List<Person> secondPersonList=new List<Person>();
		Person p3=new Person(100,"James");
		Person p4=new Person(200,"Smith");
		Person p5=new Person(300,"Thomas");
		secondPersonList.Add(p3);
		secondPersonList.Add(p4);
		secondPersonList.Add(p5);
		
		setOfPersons.Add(secondPersonList);
		
		List<Person> thirdPersonList=new List<Person>();
		Person p6=new Person(100,"James");
		Person p7=new Person(400,"Amy");
		thirdPersonList.Add(p6);
		thirdPersonList.Add(p7);
		
		setOfPersons.Add(thirdPersonList);
		
		var query = setOfPersons
            .SelectMany(l => l.Select(l1 => l1))
            .GroupBy(p => p.Id)
            .Where(g => g.Count() == setOfPersons.Count);

        Console.WriteLine("These people appears in all set:");

        foreach (var a in query)
        {
            Console.WriteLine("Id: {0} Name: {2}", a.Key, a.Count(), a.First().Name);
        }
		
	}
}


public class Person {
	public Person(int id, string name) {
		Id = id;
		Name = name;
	}
	
	public int Id { get; set; }
	public string Name { get; set; }
}
;