public void RunDemo() { IEnumerable <Singer> singers = DemoCollections.GetSingers(); Singer singer = singers.FirstOrDefault(s => s.Id == 100); Console.WriteLine(singer == null ? "No such singer" : singer.LastName); }
private void RunSecondPrototype() { IEnumerable <Singer> singers = DemoCollections.GetSingers(); IEnumerable <Concert> concerts = DemoCollections.GetConcerts(); SingerGenderComparer comparer = new SingerGenderComparer(); IEnumerable <IGrouping <int, Concert> > concertGroups = concerts.GroupBy(c => c.SingerId, comparer); foreach (IGrouping <int, Concert> concertGroup in concertGroups) { Console.WriteLine("Concerts of {0} singers: ", comparer.IsPerformedByFemaleSinger(concertGroup.Key) ? "female" : "male"); foreach (Concert concert in concertGroup) { Console.WriteLine("Number of concerts: {0}, in the year of {1} by singer {2}.", concert.ConcertCount, concert.Year, concert.SingerId); } } IEnumerable <IGrouping <int, int> > concertGroupsFiltered = concerts.GroupBy(c => c.SingerId, c => c.ConcertCount, comparer); foreach (IGrouping <int, int> concertGroup in concertGroupsFiltered) { Console.WriteLine("Concerts of {0} singers: ", comparer.IsPerformedByFemaleSinger(concertGroup.Key) ? "female" : "male"); foreach (int concertCount in concertGroup) { Console.WriteLine("Number of concerts: {0}.", concertCount); } } }
private void RunFirstPrototype() { IEnumerable <Singer> singers = DemoCollections.GetSingers(); IEnumerable <Concert> concerts = DemoCollections.GetConcerts(); IEnumerable <IGrouping <int, Concert> > concertGroups = concerts.GroupBy(c => c.SingerId); foreach (IGrouping <int, Concert> concertGroup in concertGroups) { Console.WriteLine("Concerts for singer of ID {0}:", concertGroup.Key); foreach (Concert concert in concertGroup) { Console.WriteLine("Number of concerts: {0}, in the year of {1}.", concert.ConcertCount, concert.Year); } } IEnumerable <IGrouping <int, int> > concertGroupsFiltered = concerts.GroupBy(c => c.SingerId, c => c.ConcertCount); foreach (IGrouping <int, int> concertGroup in concertGroupsFiltered) { Console.WriteLine("Concerts for singer of ID {0}:", concertGroup.Key); foreach (int concertCount in concertGroup) { Console.WriteLine("Number of concerts: {0}.", concertCount); } } }
public void RunDemo() { IEnumerable <Singer> singers = DemoCollections.GetSingers(); Dictionary <int, Singer> singersDictionary = singers.ToDictionary(s => s.Id); Console.WriteLine(singersDictionary[2].FirstName); Dictionary <int, string> singersDictionaryNames = singers.ToDictionary(s => s.Id, si => string.Format("Last name: {0}", si.LastName)); Console.WriteLine(singersDictionaryNames[2]); }
public void RunSelectManyOperatorExampleComplex() { IEnumerable <Singer> singers = DemoCollections.GetSingers(); IEnumerable <Concert> concerts = DemoCollections.GetConcerts(); var singerConcerts = singers.SelectMany(s => concerts.Where(c => c.SingerId == s.Id) .Select(c => new { Year = c.Year, ConcertCount = c.ConcertCount, Name = string.Concat(s.FirstName, " ", s.LastName) })); foreach (var item in singerConcerts) { Console.WriteLine(string.Concat(item.Name, ", ", item.Year, ", ", item.ConcertCount)); } }
public void RunDemo() { string[] bands = DemoCollections.GetBands(); string[] bandsTwo = DemoCollections.GetBands(); bool equal = bands.SequenceEqual(bandsTwo); Console.WriteLine(equal); IEnumerable <Singer> singers = DemoCollections.GetSingers(); IEnumerable <Singer> singersTwo = DemoCollections.GetSingers(); bool singersEqual = singers.SequenceEqual(singersTwo, new DefaultSingerComparer()); Console.WriteLine(singersEqual); }
public void RunDemo() { IEnumerable <Singer> singers = DemoCollections.GetSingers(); IEnumerable <Concert> concerts = DemoCollections.GetConcerts(); var singerConcerts = singers.Join(concerts, s => s.Id, c => c.SingerId, (s, c) => new { Id = s.Id, SingerName = string.Concat(s.FirstName, " ", s.LastName), ConcertCount = c.ConcertCount, Year = c.Year }); foreach (var res in singerConcerts) { Console.WriteLine(string.Concat(res.Id, ": ", res.SingerName, ", ", res.Year, ", ", res.ConcertCount)); } }
public void RunDemo() { IEnumerable <Singer> singers = DemoCollections.GetSingers(); ILookup <int, Singer> singersByBirthYear = singers.ToLookup(s => s.BirthYear); IEnumerable <Singer> filtered = singersByBirthYear[1964]; foreach (Singer s in filtered) { Console.WriteLine(s.LastName); } ILookup <int, string> singerNamesByBirthYear = singers.ToLookup(s => s.BirthYear, si => string.Concat(si.LastName, ", ", si.FirstName)); IEnumerable <string> filtered2 = singerNamesByBirthYear[1964]; foreach (string s in filtered2) { Console.WriteLine(s); } }
public void RunDemo() { List <int> integers = new List <int>() { 54, 23, 76, 123, 93, 7, 3489 }; int intMin = integers.Min(); string[] bands = { "ACDC", "Queen", "Aerosmith", "Iron Maiden", "Megadeth", "Metallica", "Cream", "Oasis", "Abba", "Blur" , "Chic", "Eurythmics", "Genesis", "INXS", "Midnight Oil", "Kent", "Madness", "Manic Street Preachers" , "Noir Desir", "The Offspring", "Pink Floyd", "Rammstein", "Red Hot Chili Peppers", "Tears for Fears" , "Deep Purple", "KISS" }; string stringMin = bands.Min(); IEnumerable <Singer> singers = DemoCollections.GetSingers(); int minSingerBirthYear = singers.Min(s => s.BirthYear); String minSingerName = singers.Min(s => s.FirstName); }
public void RunDemo() { string[] bands = { "ACDC", "Queen", "Aerosmith", "Iron Maiden", "Megadeth", "Metallica", "Cream", "Oasis" , "Abba", "Blur", "Chic", "Eurythmics", "Genesis", "INXS", "Midnight Oil","Kent", "Madness", "Manic Street Preachers" , "Noir Desir", "The Offspring", "Pink Floyd", "Rammstein", "Red Hot Chili Peppers", "Tears for Fears" , "Deep Purple", "KISS" }; bool contains = bands.Contains("Queen"); Console.WriteLine(contains); IEnumerable <Singer> singers = DemoCollections.GetSingers(); Singer s = new Singer() { Id = 2, FirstName = "Elvis", LastName = "Presley", BirthYear = 1954 }; bool containsSinger = singers.Contains(s, new DefaultSingerComparer()); Console.WriteLine(containsSinger); }
public void RunDemo() { string[] bands = DemoCollections.GetBands(); IEnumerable <string> bandsDuplicated = bands.Concat(bands); IEnumerable <string> uniqueBands = bandsDuplicated.Distinct(); foreach (String unique in uniqueBands) { Console.WriteLine(unique); } IEnumerable <Singer> singers = DemoCollections.GetSingers(); IEnumerable <Singer> singersDuplicates = singers.Concat(singers); IEnumerable <Singer> uniqueSingers = singersDuplicates.Distinct(new DefaultSingerComparer()); foreach (Singer singer in uniqueSingers) { Console.WriteLine(singer.Id); } }