예제 #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();
            }
        }
예제 #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;
 }
예제 #3
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);
            }
        }
예제 #4
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);
                }
            }
        }
예제 #5
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);
            }
        }
예제 #6
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()
     };
 }
예제 #7
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}");
            }
        }
예제 #8
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
     });
 }
예제 #9
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
     };
 }
예제 #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
     });
 }
예제 #11
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}");
            }
        }
예제 #12
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);
                }
            }
        }
예제 #13
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}");
            }
        }
예제 #14
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)
     });
예제 #15
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);
            }
        }
예제 #16
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);
 }