Exemplo n.º 1
0
        private static void ElementOperators()
        {
            Console.WriteLine("Elements");
            List <F1Team> teams = F1Data.GetTeams();

            F1Team team = teams.ElementAt(0);

            team = teams.First(t => t.TeamName.Contains("a"));
            team = teams.Last();
            team = teams.SingleOrDefault(t => t.Pilots.Length == 3);
        }
Exemplo n.º 2
0
        private static void Aggregate()
        {
            Console.WriteLine("----Elements");
            List <F1Team> teams = F1Data.GetTeams();

            int countTeam = teams.Count(t => t.Wins > 0);

            var query = from team in teams
                        let points = team.Pilots.Sum(p => p.Points)
                                     orderby points descending
                                     select new { team.TeamName, points };

            Dump(query);
            int sumPoints = teams.Where(t => t.TeamName.Contains("Ferrari")).SelectMany(t => t.Pilots).Sum(p => p.Points);


            int[] array = { 1, 2, 3, 4, 5 };
            int   sum1  = array.Sum();

            var queryAverage = from team in teams
                               select new { team.TeamName, AvgPoints = team.Pilots.Average(p => p.Points) };

            Dump(queryAverage);

            int max = (from team in teams
                       from pilot in team.Pilots
                       select pilot.Points).Max();

            Console.WriteLine("max={0}", max);

            int min = (from team in teams
                       from pilot in team.Pilots
                       select pilot).Min(p => p.Points);

            Console.WriteLine("max={0}", max);

            string first = (from team in teams
                            select team.TeamName).Min();

            string frase    = "ti voglio tanto bene"; //esempio dedicato alla mia amica Francesca
            var    acronimo = frase.Split(' ').Aggregate("", (result, word) => result + word.ToUpper().First() + ".");

            Console.WriteLine(acronimo); //stampa TVTB

            var queryAggregate = from team in teams
                                 select new { Points = team.Pilots.Aggregate(0, (total, p) => total + p.Points) };

            Dump(queryAggregate);
        }
Exemplo n.º 3
0
        public static void IntoClause()
        {
            Console.WriteLine("---- Into Clause");
            List <F1Team> teams = F1Data.GetTeams();

            var query = from team in teams
                        where team.Wins > 2
                        select team.Pilots
                        into topTeamPilots
                        from tp in topTeamPilots
                        where tp.Points > 50
                        orderby tp.Points descending
                        select tp.LastName + " " + tp.FirstName + ": " + tp.Points;

            Dump(query);
        }
Exemplo n.º 4
0
        public static void Quantifier()
        {
            Console.WriteLine("Quantifiers");
            var teams = F1Data.GetTeams();

            int[] array    = { 1, 2, 3, 4 };
            bool  contains = array.Contains(1);

            bool allpositive = array.All(i => i > 0);
            var  query       = teams.Where(t => t.Pilots.All(p => p.Points > 0));

            Console.WriteLine("All");
            Dump(query);

            var queryAny = teams.Where(t => t.Pilots.Any(p => p.Points > 100));

            Console.WriteLine("Any");
            Dump(queryAny);
        }
Exemplo n.º 5
0
        private static void Conversions()
        {
            Console.WriteLine("Elements");
            List <F1Team> teams = F1Data.GetTeams();


            var query1 = teams.Where(t => t.Wins > 0).AsEnumerable();

            List <F1Team> list = teams.Where(t => t.Wins > 0).ToList();

            Pilot[] array = teams.SelectMany(t => t.Pilots).ToArray();

            Dictionary <string, Pilot[]> dict = (from team in teams
                                                 where team.Wins > 0
                                                 select team).ToDictionary(t => t.TeamName, t => t.Pilots);

            foreach (string key in dict.Keys)
            {
                Console.WriteLine(key);
                foreach (Pilot p in dict[key])
                {
                    Console.WriteLine("- {0}: {1}", p.LastName, p.Points);
                }
            }

            ILookup <F1Team, Pilot[]> lookup = (from team in teams
                                                where team.Wins > 0
                                                select team).ToLookup(t => t, t => t.Pilots);

            Dump(lookup);

            List <object> lista = new List <object>()
            {
                "a", 1, "b", 2, "c"
            };
            var stringhe = lista.OfType <string>();

            int[] intArray = { 1, 2, 3, 4, 5 };
            var   objArray = intArray.Cast <object>();
        }
Exemplo n.º 6
0
        public static void Grouping()
        {
            Console.WriteLine("---- grouping");
            List <F1Team> teams = F1Data.GetTeams();

            var query = from team in teams
                        from pilot in team.Pilots
                        group pilot by team;

            foreach (var group in query)
            {
                Console.WriteLine(group.Key);
                foreach (var pilot in group)
                {
                    Console.WriteLine(" - {0} {1}", pilot.FirstName, pilot.LastName);
                }
            }

            foreach (IGrouping <F1Team, Pilot> group in query)
            {
                F1Team team = group.Key;
                Console.WriteLine(team);
                foreach (Pilot pilot in group)
                {
                    Console.WriteLine(" - {0} {1}", pilot.FirstName, pilot.LastName);
                }
            }


            var query2 = from team in teams
                         from pilot in team.Pilots
                         group pilot by team into t
                         select new { t.Key, t };

            foreach (var pilot in query2)
            {
                Console.WriteLine(pilot.Key);
            }
        }
Exemplo n.º 7
0
        public static void LetClause()
        {
            Console.WriteLine("---- let");
            List <F1Team> teams = F1Data.GetTeams();

            var query = from team in teams
                        let wins = team.Wins
                                   orderby wins descending
                                   where wins > 3
                                   select team.TeamName + ": " + wins;

            Dump(query);

            query                       = from team in teams
                               let wins = team.Wins
                                          let pilots = team.Pilots
                                                       from pilot in pilots
                                                       where pilot.Points > 80
                                                       orderby wins descending
                                                       select team.TeamName + " wins: " + wins + ", leader " + pilot.LastName + " " + pilot.Points;

            Dump(query);
        }
Exemplo n.º 8
0
        public static void Filtering()
        {
            Console.WriteLine("Filtering");
            List <F1Team> teams = F1Data.GetTeams();
            var           query = from team in teams
                                  where team.Wins > 0 &&
                                  team.TeamName.ToLower().StartsWith("m")
                                  select team;

            query.Count();
            Dump(query);

            Console.WriteLine("Where<T>");
            var query2 = teams.Where(team => team.Wins > 0 &&
                                     team.TeamName.ToLower().StartsWith("m"));

            Dump(query2);

            Console.WriteLine("Where<T,index>");
            var query3 = teams.Where((team, index) => index % 2 == 0);

            Dump(query3);
        }
Exemplo n.º 9
0
        private static void Generators()
        {
            Console.WriteLine("----Generation");
            var teams = F1Data.GetTeams();


            var months = Enumerable.Range(1, 12).Select(n => CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(n));

            months.ToList().ForEach(Console.WriteLine);

            int[] array = Enumerable.Repeat <int>(1, 10).ToArray();

            var expr = Enumerable.Repeat((from team in teams select team.TeamName), 10);

            IEnumerable <Pilot> pilots = Enumerable.Empty <Pilot>();

            foreach (var pilot in pilots.DefaultIfEmpty(new Pilot()
            {
                LastName = "Sconosciuto"
            }))
            {
                Console.WriteLine(pilot.LastName);
            }
        }
Exemplo n.º 10
0
        public static void Join()
        {
            Console.WriteLine("---- join");
            List <F1Team> teams = F1Data.GetTeams();

            var query = from team in teams
                        from pilot in team.Pilots
                        join country in Country.All on pilot.IDCountry equals country.IDCountry
                        select new { pilot.LastName, CountryName = country.Name };


            foreach (var p in query)
            {
                Console.WriteLine("{0} ({1})", p.LastName, p.CountryName);
            }

            Console.WriteLine("----Join method");
            var query2 = teams.SelectMany(t => t.Pilots).Join(
                Country.All,
                pilot => pilot.IDCountry,
                country => country.IDCountry,
                (p, c) => new { p.LastName, CountryName = c.Name });

            foreach (var p in query2)
            {
                Console.WriteLine("{0} ({1})", p.LastName, p.CountryName);
            }

            Console.WriteLine("----join into");
            var pilots = from team in teams
                         from pilot in team.Pilots
                         select pilot;

            var query3 = from country in Country.All
                         join pilot in pilots on country.IDCountry equals pilot.IDCountry
                         into pilotsxCountry
                         select new { CountryName = country.Name, Pilots = pilotsxCountry };

            foreach (var group in query3)
            {
                Console.WriteLine(group.CountryName);
                foreach (Pilot pilot in group.Pilots)
                {
                    Console.WriteLine("   {0} {1}", pilot.LastName, pilot.FirstName);
                }
            }

            Console.WriteLine("---GroupJoin");
            IEnumerable <Pilot> pilots2 = teams.SelectMany(t => t.Pilots);

            var query4 = Country.All.GroupJoin(
                pilots2,
                country => country.IDCountry,
                pilot => pilot.IDCountry,
                (c, p) => new { CountryName = c.Name, Pilots = p });


            foreach (var group in query4)
            {
                Console.WriteLine(group.CountryName);
                foreach (Pilot pilot in group.Pilots)
                {
                    Console.WriteLine("   {0} {1}", pilot.LastName, pilot.FirstName);
                }
            }
        }