public void RunNewExamples() { IEnumerable <WebPage> webPages = DemoCollections.GetWebPages(); List <PageComponent> allPageComponents = webPages.SelectMany(wps => wps.PageComponents).ToList(); //allPageComponents.ForEach(pc => Console.WriteLine(pc.ToString())); List <char> chars = webPages.SelectMany(wps => wps.Name).ToList(); //chars.ForEach(c => Console.Write(c)); var uniqueExtensions = webPages.SelectMany(wps => wps.PageComponents .Select(pc => new { Extension = pc.Extension })).Distinct().OrderBy(n => n.Extension).ToList(); //uniqueExtensions.ForEach(p => Console.WriteLine(p.Extension)); var allPageComponentsWithIndex = webPages.SelectMany ((wps, index) => wps.PageComponents.Select(c => new { ComponentName = c.Name, PositionOfContainer = index })).ToList(); //allPageComponentsWithIndex.ForEach(pc => Console.WriteLine(string.Concat(pc.PositionOfContainer, ": ", pc.ComponentName))); var pagesWithComponents = webPages .SelectMany(wps => wps.PageComponents, (wp, cp) => new { PageName = wp.Name, ComponentName = cp.Name }).ToList(); //pagesWithComponents.ForEach(p => Console.WriteLine(string.Concat(p.PageName, ": ", p.ComponentName))); var pagesWithComponentsAndIndex = webPages.SelectMany((wps, index) => wps.PageComponents .Select(c => new { ComponentName = c.Name, PositionOfContainer = index }), (wp, cp) => new { PageName = wp.Name, ComponentName = cp.ComponentName, PositionOfContainer = cp.PositionOfContainer }).ToList(); pagesWithComponentsAndIndex.ForEach(c => Console.WriteLine(string.Concat("Page: ", c.PageName, ", position: ", c.PositionOfContainer, ", component: ", c.ComponentName))); }
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 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); } } }
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); } } }
public void RunDemo() { string[] bands = DemoCollections.GetBands(); bool all = bands.All(b => b.Length > 2); Console.WriteLine(all); }
public void RunDemo() { string[] bands = DemoCollections.GetBands(); IEnumerable <string> orderedOne = bands.OrderBy(band => band.Length); foreach (string item in orderedOne) { //Console.WriteLine(item); } IEnumerable <string> orderedTwo = bands.OrderBy(band => band, new ConsonantNumberComparer()); foreach (string item in orderedTwo) { //Console.WriteLine(item); } IEnumerable <string> orderedThree = bands.OrderBy(band => band, new ConsonantNumberComparer()).ThenBy(band => band); foreach (string item in orderedThree) { //Console.WriteLine(item); } IEnumerable <string> ordered = bands.OrderByDescending(band => band, new ConsonantNumberComparer()).ThenByDescending(band => band); foreach (string item in ordered) { Console.WriteLine(item); } }
public void RunDemo() { string[] bands = DemoCollections.GetBands(); bool exists = bands.Any(); //bool exists = bands.Any(b => b.EndsWith("hkhkj")); Console.WriteLine(exists); }
public void RunDemo() { string[] bands = DemoCollections.GetBands(); IEnumerable <string> bandsReversed = bands.Reverse(); foreach (string item in bandsReversed) { Console.WriteLine(item); } }
public void RunDemo() { string[] bands = DemoCollections.GetBands(); IEnumerable <string> res = bands.Skip(10); foreach (string item in res) { Console.WriteLine(item); } }
public void RunDemo() { string[] bands = DemoCollections.GetBands(); IEnumerable <string> selectedItems = bands.Take(5).Concat(bands.Reverse().Take(5)); foreach (string item in selectedItems) { Console.WriteLine(item); } }
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 RunSelectManyOperatorExample() { string[] bands = DemoCollections.GetBands(); IEnumerable <char> characters = bands.SelectMany(b => b.ToArray()); foreach (char item in characters) { Console.WriteLine(item); } }
public void RunDemo() { IEnumerable <int> integers = Enumerable.Range(1, 100); int sum = integers.Sum(); Console.WriteLine(sum); IEnumerable <Concert> concerts = DemoCollections.GetConcerts(); int allConcerts = concerts.Sum(c => c.ConcertCount); Console.WriteLine(allConcerts); }
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() { string[] bands = DemoCollections.GetBands(); IEnumerable <string> res = bands.TakeWhile(b => b[0] != 'E'); foreach (string s in res) { //Console.WriteLine(s); } IEnumerable <string> res2 = bands.TakeWhile((b, i) => b[0] != 'E' && i < 8); foreach (string s in res2) { Console.WriteLine(s); } }
public void RunDemo() { string[] bands = DemoCollections.GetBands(); IEnumerable <string> res = bands.SkipWhile(s => s.Length < 10); foreach (string item in res) { //Console.WriteLine(item); } IEnumerable <string> res2 = bands.SkipWhile((s, i) => s.Length < 10 && i < 10); foreach (string item in res2) { Console.WriteLine(item); } }
public void RunTakeDemo() { string[] bands = DemoCollections.GetBands(); IEnumerable <String> topItems = bands.Take(10); foreach (string item in topItems) { //Console.WriteLine(item); } var anonymous = bands.Take(10).Select(b => new { Name = b, Length = b.Length }); foreach (var anon in anonymous) { Console.WriteLine("Band name: {0}, length: {1}", anon.Name, anon.Length); } }
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() { 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() { 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() { 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); } }
public void RunSelectOperatorExample() { string[] bands = DemoCollections.GetBands(); IEnumerable <int> lengths = bands.Select(b => b.Length); foreach (int l in lengths) { Console.WriteLine(l); } var customObjects = bands.Select(b => new { Name = b, Length = b.Length }); foreach (var item in customObjects) { Console.WriteLine("Band name: {0}, length: {1}", item.Name, item.Length); } IEnumerable <Band> bandListSimple = bands.Select(b => new Band() { AllCapitals = b.ToUpper(), Name = b, NameLength = b.Length }); foreach (Band band in bandListSimple) { Console.WriteLine(string.Concat(band.Name, ", ", band.NameLength, ", ", band.AllCapitals)); } IEnumerable <Band> bandList = bands.Select((b, i) => new Band() { AllCapitals = b.ToUpper(), BandIndex = i + 1, Name = b, NameLength = b.Length }); foreach (Band band in bandList) { Console.WriteLine(string.Concat(band.BandIndex, ": ", band.Name, ", ", band.NameLength, ", ", band.AllCapitals)); } }
public void RunDemo() { /* * IEnumerable<Singer> singers = DemoCollections.GetSingers(); * IEnumerable<Concert> concerts = DemoCollections.GetConcerts(); * * var singerConcerts = singers.GroupJoin(concerts, s => s.Id, c => c.SingerId, (s, co) => new * { * Id = s.Id * , SingerName = string.Concat(s.FirstName, " ", s.LastName) * , ConcertCount = co.Sum(c => c.ConcertCount) * }); * * foreach (var res in singerConcerts) * { * Console.WriteLine(string.Concat(res.Id, ": ", res.SingerName, ", ", res.ConcertCount)); * } */ //1st silly try IEnumerable <Student> students = DemoCollections.GetStudents(); IEnumerable <StudentScore> studentScores = DemoCollections.GetScores(); IEnumerable <IGrouping <string, StudentScore> > subjectGroupings = studentScores .GroupBy(score => score.Subject); foreach (var subjectGroup in subjectGroupings) { Console.WriteLine($"Subject: {subjectGroup.Key}"); Console.WriteLine($"Total points across all students: {subjectGroup.Sum(g => g.Points)}"); } var totalStudentScores = students.GroupJoin(studentScores , student => student.Id, score => score.StudentId, (student, scores) => new { Id = student.Id, StudentName = student.Name, TotalScore = scores.Sum(s => s.Points) } ); foreach (var student in totalStudentScores) { Console.WriteLine($"ID: {student.Id}, Name: {student.StudentName}, Total score: {student.TotalScore} "); } //grouped try var groupedStudentScores = students.GroupJoin(studentScores , student => student.Id, score => score.StudentId, (student, scores) => { var subjectGroups = scores.Where(score => score.StudentId == student.Id) .GroupBy(score => score.Subject); Dictionary <string, int> scoresDictionary = new Dictionary <string, int>(); foreach (var subjectGroup in subjectGroups) { string subjectName = subjectGroup.Key; int subjectScoreTotal = subjectGroup.Sum(s => s.Points); scoresDictionary[subjectName] = subjectScoreTotal; } return(new { Id = student.Id, StudentName = student.Name, Scores = scoresDictionary }); } ); foreach (var student in groupedStudentScores) { Console.WriteLine($"ID: {student.Id}, Name: {student.StudentName}, Total score by subject: {JsonConvert.SerializeObject(student.Scores)} "); } }
private void RunVariousExamples() { IEnumerable <PageComponent> pageComponents = DemoCollections.GetPageComponents(); //group by type IEnumerable <IGrouping <string, PageComponent> > groupByTypeBasic = pageComponents.GroupBy(g => g.Type); foreach (var group in groupByTypeBasic) { /* * Console.WriteLine($"Element type: {group.Key}, items in group: {group.Count()}, total size: {group.Sum(g => g.SizeBytes)}"); * Console.WriteLine("============================"); * foreach (PageComponent pc in group) * { * Console.WriteLine($"Element name: {pc.Name}"); * } * Console.WriteLine("============================"); * Console.WriteLine(); */ } //group by extension IEnumerable <IGrouping <string, PageComponent> > groupByExtensionBasic = pageComponents.GroupBy(g => g.Extension); foreach (var group in groupByExtensionBasic) { /* * Console.WriteLine($"Extension: {group.Key}"); * Console.WriteLine("============================"); * foreach (PageComponent pc in group) * { * Console.WriteLine($"Element name: {pc.Name}"); * } * Console.WriteLine("============================"); * Console.WriteLine(); */ } var groupByComponentSizeRange = pageComponents.GroupBy(pe => { int groupSize = 10000; int lowerBound = pe.SizeBytes - pe.SizeBytes % groupSize; int upperBound = lowerBound + groupSize; return(new PageComponentSizeGrouper() { LowerBound = lowerBound, UpperBound = upperBound }); }, new PageComponentSizeGrouperEqualityComparer()); foreach (var group in groupByComponentSizeRange.OrderBy(g => g.Key.LowerBound)) { /* * Console.WriteLine($"Size range: {group.Key.ToString()}, items in group: {group.Count()}"); * Console.WriteLine("============================"); * foreach (PageComponent pc in group) * { * Console.WriteLine($"Element name: {pc.Name}"); * } * Console.WriteLine("============================"); * Console.WriteLine(); */ } var duplicatesFinder = pageComponents.GroupBy(pe => pe, new PageElementEqualityComparer()); foreach (var group in duplicatesFinder) { //Console.WriteLine($"Element name: {group.Key.Name}, occurrence: {group.Count()}"); } var groupByTypeWithElementSelector = pageComponents.GroupBy( pe => pe.Type, pe => new { NameCapitals = pe.Name.ToUpper(), ShortName = pe.Name.Substring(0, 5), CharCount = pe.Name.Count() }); foreach (var group in groupByTypeWithElementSelector) { /* * Console.WriteLine($"Element type: {group.Key}"); * Console.WriteLine("============================"); * foreach (var pc in group) * { * Console.WriteLine($"Element name capitals: {pc.NameCapitals}, short name: {pc.ShortName}, chars: {pc.CharCount}"); * } * Console.WriteLine("============================"); * Console.WriteLine(); */ } var groupByComponentSizeWithResultSelector = pageComponents.GroupBy(pe => { int groupSize = 10000; int lowerBound = pe.SizeBytes - pe.SizeBytes % groupSize; int upperBound = lowerBound + groupSize; return(new PageComponentSizeGrouper() { LowerBound = lowerBound, UpperBound = upperBound }); }, (sizeGroup, pageElements) => new { ElementCountInGroup = pageElements.Count(), MinElementSize = pageElements.Min(pe => pe.SizeBytes), MaxElementSize = pageElements.Max(pe => pe.SizeBytes), GroupingKeyOrderer = sizeGroup.LowerBound, GroupingKeyToString = sizeGroup.ToString() } , new PageComponentSizeGrouperEqualityComparer()); foreach (var group in groupByComponentSizeWithResultSelector.OrderBy(g => g.GroupingKeyOrderer)) { /* * Console.WriteLine($"Size range: {group.GroupingKeyToString}"); * Console.WriteLine($"Item count in group: { group.ElementCountInGroup}"); * Console.WriteLine($"Min element size: {group.MinElementSize}"); * Console.WriteLine($"Max element size: {group.MaxElementSize}"); * Console.WriteLine("============================"); * Console.WriteLine(); */ } var groupByComponentSizeWithResultAndElementSelector = pageComponents.GroupBy(pe => { int groupSize = 10000; int lowerBound = pe.SizeBytes - pe.SizeBytes % groupSize; int upperBound = lowerBound + groupSize; return(new PageComponentSizeGrouper() { LowerBound = lowerBound, UpperBound = upperBound }); }, pe => new { ElementName = pe.Name, ModifiedSize = pe.SizeBytes - 1000 }, (sizeGroup, pageElements) => { int minElementSize = pageElements.Min(pe => pe.ModifiedSize); int maxElementSize = pageElements.Max(pe => pe.ModifiedSize); string minElementName = (from pe in pageElements where pe.ModifiedSize == minElementSize select pe.ElementName).FirstOrDefault(); string maxElementName = (from pe in pageElements where pe.ModifiedSize == maxElementSize select pe.ElementName).FirstOrDefault(); return(new { ElementCountInGroup = pageElements.Count(), MinElementSize = minElementSize, MaxElementSize = maxElementSize, MinElementName = minElementName, MaxElementName = maxElementName, GroupingKeyOrderer = sizeGroup.LowerBound, GroupingKeyToString = sizeGroup.ToString() }); } , new PageComponentSizeGrouperEqualityComparer()); foreach (var group in groupByComponentSizeWithResultAndElementSelector.OrderBy(g => g.GroupingKeyOrderer)) { Console.WriteLine($"Size range: {group.GroupingKeyToString}"); Console.WriteLine($"Item count in group: { group.ElementCountInGroup}"); Console.WriteLine($"Min element size: {group.MinElementSize}"); Console.WriteLine($"Max element size: {group.MaxElementSize}"); Console.WriteLine($"Min element name: {group.MinElementName}"); Console.WriteLine($"Max element name: {group.MaxElementName}"); Console.WriteLine("============================"); Console.WriteLine(); } }