linq/ex4A.cs - Same - using flattening SelecteMany. | Lecture 16 - slide 4 : 11 Program 6 |
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 pairs = males // SelectMany flattens the nested sequences .SelectMany(m => females .Select(f => new{First=f, Second=m})); foreach(var pair in pairs) Console.WriteLine("{0} <-> {1}", pair.First, pair.Second); } }