Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 3
0
 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;
 }
Ejemplo n.º 4
0
        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);
            }
        }
Ejemplo n.º 5
0
 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()
     };
 }
Ejemplo n.º 6
0
        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);
            }
        }
Ejemplo n.º 7
0
        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);
                }
            }
        }
Ejemplo n.º 8
0
        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}");
            }
        }
Ejemplo n.º 9
0
 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
     });
 }
Ejemplo n.º 10
0
 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
     });
 }
Ejemplo n.º 11
0
 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
     };
 }
Ejemplo n.º 12
0
        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}");
            }
        }
Ejemplo n.º 13
0
        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);
                }
            }
        }
Ejemplo n.º 14
0
        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}");
            }
        }
Ejemplo n.º 15
0
 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)
     });
Ejemplo n.º 16
0
        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);
            }
        }
Ejemplo n.º 17
0
 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);
 }