linq/ex4.cs - A sequence of female-male pairs sequences. | Lecture 16 - slide 4 : 11 Program 5 |
using System; using System.Collections.Generic; using System.Linq; public class Example1{ public static void Main(){ // A collection of all possible combination of female-male pairs IEnumerable<Person> males = Person.SomePersons .Where(p => p.Sex == Sex.Male); IEnumerable<Person> females = Person.SomePersons .Where(p => p.Sex == Sex.Female); var pairsPerMale = males // A sequence of sequences .Select(m => females .Select(f => new{First=f, Second=m})); foreach(var pairs in pairsPerMale){ foreach(var pair in pairs) Console.WriteLine("{0} <-> {1}", pair.First, pair.Second); } } }