static void GroupingAndNestedObjects() { var countries = from r in Formulal.GetChampions() group r by r.Country into g let count = g.Count() orderby count descending, g.Key where count >= 2 select new { Country = g.Key, Count = coung, Racers = from r1 in g orderby r1.LastName select r1.FirstName + " " + r1.LastName }; foreach (var item in countries) { Console.WriteLine($"{item.Country,-10}{item.Count}"); foreach (var name in item.Racers) { Console.WriteLine(name); } Console.WriteLine(); } }
static void CompoundFrom() { var ferrariDrivers = from r in Formulal.GetChampions() from c in r.Cars where c == "F" orderby r.LastName select r.FirstName + " " + LastName; }
static void GroupJoin() { var racers = from cs in Formulal.GetContructorChampions() from r in new List <(int Year, int Position, string FirstName, string LastName)>() { (cs.Year, 1, cs.First.LastName()) } select r; }
static void Filtering() { var racers = from r in Formulal.GetChampions() where r.Wins > 15 && (r.Country == "B" || r.Country == "A") select r; foreach (var item in racers) { Console.WriteLine(item); } }
static void Grouping() { var countries = from r in Formulal.GetChampions() group r by r.Country into g orderby g.Count() descending, g.key where g.Count() >= 2 select new { Country = g.Key, Count = g.Count() }; }
static void ToList() { List <Racer> racers = (from r in Formulal.GetChampions() where r.Statrts > 200 orderby r.Starts descending select r).ToList(); foreach (var item in racers) { Console.WriteLine(item); } }
static void ToLookup() { var racers = (from r in Formulal.GetChampions() from c in r.Cars select new { Car = c, Racer = r }).ToLookup(cr => cr.Car, cr => cr.Racer); if (racers.Contains("Williams")) { foreach (var item in racers["Williams"]) { Console.WriteLine(item); } } }
static void LinqQuery() { var query = from r in Formulal.GetChampions() where r.Country == "Brazil" orderby r.Wins descending select r; foreach (var r in query) { Console.WriteLine($"{r:A}"); } }
static void GroupingWithAnonymousTypes() { var countries = Formulal.GetChampions().GroupBy(r => r.Country) .Select(g => new { Group = g, Count = g.Count() }) .OrderByDescending(g => g.Count) .ThenBy(g => g.Group.Key) .Where(g => g.Count >= 2) .Select(g => new { Country = g.Group.Key, Count = g.Count }); }
static void GroupingWithMethods() { var countries = Formulal.GetChampions() .GroupBy(r => r.Country) .OrdertByDescending(g => g.Count()) .ThenBy(g => g.Key) .Where(g => g.Count() >= 2) .Select(g => new { Country = g.Key, Count = g.Count }); }
static void GroupingWithVariables() { var countries = from r in Formulal.GetChampions() group r by r.Country into g let count = g.Count() orderby count descending, g.key where count >= 2 select new { Country = g.Key, Count = count }; }
static void ConverWithCast() { var list = new System.Conllections.ArrayList(Formulal.GetChampions() as System.Collections.ICollection); var query = from r in list.Cast <Racer>() where r.Country == "USA" orderby r.Wins descending select r; foreach (var item in query) { Console.WriteLine($"{item:A}"); } }
static void Partitioning() { int pageSize = 5; int numberPages = (int)Math.Ceiling(Formulal.GetChampions().Count() / (double)pageSize); for (int i = 0; i < numberPages; i++) { Console.WriteLine(i); var racers = (from r in Formulal.GetChampions() orderby r.LastName, r.FirstName select r.FirstName + " " + r.LastName).Skip(pageSize * pageSize).Take(pageSize); foreach (var item in racers) { Console.WriteLine(item); } } }
static void AggregateCount() { var query = from r in Formulal.GetChampions() let numberYears = r.Years.Count() where numberYears >= 3 orderby numberYears descending, r.LastName select new { Name = r.FirstName + " " + r.LastName, TimeChampion = numberYears }; foreach (var item in query) { Console.WriteLine($"{item.Name}{item.TimeChampion}"); } }
static void GroupingAndNestedObjectsWithMethods() { var countries = Formulal.GetChampions().GroupBy(r => r.Country) .Select(g => new { Group = g, Key = g.Key, Count = g.Count() }) .OrderByDescending(g => g.Count) .ThenBy(g => g.Key) .Where(g => g.Count >= 2) .Select(g => new { Country = g.Key, Count = g.Count, Racers = g.Group.OrderBy(r => r.LastName), .Select(r => r.FirstName + " " + r.LastName) });
static void ExtensionMethods() { var champions = new List <Racer>(Formulal.GetChampions()); IEnumerable <Racer> brazilChampions = champions.Where(r => r.Country == "Brazil").OrderByDescending(r => r.Wins).Select(r => r); foreach (var item in brazilChampions) { Console.WriteLine($"{item:A}"); } var names = new List <string> { "a", "B", "C", "d" }; var namesWithJ = (from n in names where n.StartsWith("a") orderby n select n).ToList(); Console.WriteLine("First iteration"); foreach (string name in namesWithJ) { Console.WriteLine(name); } }
static void CompoundFromWithMethods() { var ferrariDrivers = Formulal.GetChampions().SelectMany(r => r.Cars, (r, c) => new { Racer = r, Car = c }).Where(r => r.Car = "F").OrderBy(r => r.Racer.LastName).Select(r => r.Racer.FirstName + " " + r.Racer.LastName); }