- using System;
- using System.Collections.Generic;
- using System.Text;
- public static class myEnumerable
- {
- public static IEnumerable<S> SelectMany<T, S>(
- this IEnumerable<T> source, Func<T, IEnumerable<S>> selector)
- {
- foreach (T item in source)
- {
- IEnumerable<S> Scollect=selector(item);
- foreach (S child in Scollect)
- {
- yield return child;
- }
- }
- }
- }
- class PetOwner
- {
- public string Name { get; set; }
- public List<String> Pets { get; set; }
- }
- class ExtensionMethods
- {
- public static void Main()
- {
- PetOwner[] petOwners =
- {
- new PetOwner { Name="Higa, Sidney", Pets = new List<string>{ "Scruffy", "Sam" } },
- new PetOwner { Name="Ashkenazi, Ronen", Pets = new List<string>{ "Walker", "Sugar" } },
- new PetOwner { Name="Price, Vernette", Pets = new List<string>{ "Scratches", "Diesel" } }
- };
- IEnumerable<string> query = petOwners.SelectMany(petOwner => petOwner.Pets);
- Console.WriteLine("Using SelectMany():");
- foreach (string pet in query)
- {
- Console.WriteLine(pet);
- }
- Console.ReadKey();
- }
- }