Beispiel #1
0
        static void Main(string[] args)
        {
            #region === ESERCITAZIONE ===

            Esercizi.Esegui();

            return;

            #endregion

            #region === Step 1 ===

            Console.WriteLine("=== LINQ ===");

            string firstName = "Roberto";

            var lastName = 0.4;

            //lastName = "Ajolfi";   // ERRORE!

            //using var file = new StreamWriter(null);

            //var data = new List<int>{ 1, 2, 3, 4 };

            List <Employee <int> > data = new List <Employee <int> >
            {
                new Employee <int>()
            };

            Employee <int>    firstEmployee = new Employee <int>();
            Employee <string> secondEmployee;

            // Extension Methods
            string example = "230";
            example.ToUpper();
            Console.WriteLine(example.ToDouble());
            var prefix = example.WithPrefix("[TST]");
            Console.WriteLine(prefix);

            MyString example2 = new MyString();
            example2.Value = "Example";
            example2.Value.ToUpper();
            // OPPURE
            example2.ToUpper();


            foreach (var value in data)
            {
                Console.WriteLine("#" + value.Name);
            }

            Class1 class1 = new Class1();

            //Class2 class2 = new ClassLib.Class2();

            var person = new { firstName = "Roberto", lastName = "Ajolfi", eta = 12 };

            var person2 = new { nome = "Alice", cognome = "Colella" };

            var person3 = person2;

            firstEmployee.ID = 9;

            #endregion

            #region === Step 2 ===

            // EVENTS
            var process = new BusinessProcess();
            process.Started   += Process_Started;
            process.Started   += Process_Started1;
            process.Completed += CompletedProcess;

            process.StartedCore += Process_StartedCore;
            //process.ProcessData();

            // DELEGATES
            Sum lamiaSomma = new Sum(PrimaSomma);
            //// OPPURE
            Sum lamiaSomma2 = PrimaSomma;

            // Sum == Func<int, int, int>

            // Func and Action
            Func <int, double, int> primaFunc   = SecondaSomma;
            Func <int, int, int>    secondaFunc = PrimaSomma;

            Action <int> primaAction;

            //// ERRORE!!! Wrong Signature
            ////lamiaSomma = SecondaSomma;

            //Chiamami(lamiaSomma);
            //Chiamami(PrimaSomma);
            //// ERRORE
            //Chiamami(SecondaSomma);

            #endregion

            #region === Step 3 ===

            Func <int, int> lamdbaZero = x => 2 * x;

            Func <int, int> lamdbaZeroZero = x => {
                var result = 2 * x;
                return(result);
            };

            Func <int, int> lamdbaZeroZeroZero = Multiply;

            lamdbaZeroZero(45);

            var list = new List <int> {
                1, 2, 3, 4, 5, 6
            };
            //var results = Where(dataInt, x => x > 2);

            var results = list.Where(x => x > 2);

            Func <int, double, bool> lambdaOne = (x, y) => x > (int)y;

            #endregion

            #region === Step 4 ===

            List <EmployeeInt> employees = new List <EmployeeInt>
            {
                new EmployeeInt {
                    ID = 1, Name = "Roberto"
                },
                new EmployeeInt {
                    ID = 2, Name = "Alice"
                },
                new EmployeeInt {
                    ID = 3, Name = "Mauro"
                },
                new EmployeeInt {
                    ID = 4, Name = "Roberto"
                },
            };

            var result  = employees.Where("ID", "1");
            var result2 = employees.Where("Name", "Roberto");

            // value => value * value

            ParameterExpression y = Expression.Parameter(typeof(int), "value");
            var basettoni         = new ParameterExpression[] {
                y
            };

            Expression <Func <int, int> > squareExpression =
                Expression.Lambda <Func <int, int> >(
                    Expression.Multiply(y, y),
                    basettoni
                    );

            Expression <Func <int, int> > squareExpression2 = value => value * value;

            Func <int, int> funzione = squareExpression.Compile();
            Console.WriteLine(funzione(3));

            var emp = EmployeeInt.Empty;

            #endregion

            #region === Step 5 ===

            var products = new List <Product>
            {
                new Product {
                    ID = 1, ProductCode = "PC001"
                },
                new Product {
                    ID = 2, ProductCode = "PC001"
                },
                new Product {
                    ID = 1, ProductCode = "PC001"
                }
            };

            int resultCount1  = products.Select(s => s).Distinct().Count();
            int resultCount1a = products.Select(s => s).Distinct(new ProductComparer()).Count();
            int resultCount2  = products.Select(s => new { s.ID, s.ProductCode }).Distinct().Count();

            Console.WriteLine($"{resultCount1} - {resultCount1a} - {resultCount2}");

            #endregion

            #region === Step 6 ===

            List <Employee> objEmployee = new List <Employee>()
            {
                new Employee()
                {
                    Name = "Ashish Sharma", Department = "Marketing", Country = "India"
                },
                new Employee()
                {
                    Name = "John Smith", Department = "IT", Country = "USA"
                },
                new Employee()
                {
                    Name = "Kim Jong", Department = "Sales", Country = "China"
                },
                new Employee()
                {
                    Name = "Marcia Adams", Department = "HR", Country = "USA"
                },
                new Employee()
                {
                    Name = "John Doe", Department = "Operations", Country = "Canada"
                }
            };

            var emp1 = objEmployee.ToLookup(x => x.Country);

            Console.WriteLine("Grouping Employees by Country");
            Console.WriteLine("---------------------------------");

            foreach (var grouping in emp1)
            {
                Console.WriteLine(grouping.Key);

                // Lookup employees by Country
                foreach (var item in emp1[grouping.Key])
                {
                    Console.WriteLine("\t" + item.Name + "\t" + item.Department);
                }
            }

            #endregion
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            #region === Step 1 ===

            Console.WriteLine("=== LINQ ===");

            string firstName = "Roberto";

            var lastName = 0.4;

            //lastName = "Ajolfi";   // ERRORE!

            //using var file = new StreamWriter(null);

            //var data = new List<int>{ 1, 2, 3, 4 };

            List <Employee <int> > data = new List <Employee <int> >
            {
                new Employee <int>()
            };

            Employee <int>    firstEmployee = new Employee <int>();
            Employee <string> secondEmployee;

            // Extension Methods
            string example = "230";
            example.ToUpper();
            Console.WriteLine(example.ToDouble());
            var prefix = example.WithPrefix("[TST]");
            Console.WriteLine(prefix);

            MyString example2 = new MyString();
            example2.Value = "Example";
            example2.Value.ToUpper();
            // OPPURE
            example2.ToUpper();


            foreach (var value in data)
            {
                Console.WriteLine("#" + value.Name);
            }

            Class1 class1 = new Class1();

            //Class2 class2 = new ClassLib.Class2();

            var person = new { firstName = "Roberto", lastName = "Ajolfi", eta = 12 };

            var person2 = new { nome = "Alice", cognome = "Colella" };

            var person3 = person2;

            firstEmployee.ID = 9;

            #endregion

            #region === Step 2 ===

            // EVENTS
            var process = new BusinessProcess();
            process.Started   += Process_Started;
            process.Started   += Process_Started1;
            process.Completed += CompletedProcess;

            process.StartedCore += Process_StartedCore;
            process.ProcessData();

            // DELEGATES
            Sum lamiaSomma = new Sum(PrimaSomma);
            //// OPPURE
            Sum lamiaSomma2 = PrimaSomma;

            // Sum == Func<int, int, int>

            // Func and Action
            Func <int, double, int> primaFunc   = SecondaSomma;
            Func <int, int, int>    secondaFunc = PrimaSomma;

            Action <int> primaAction;

            //// ERRORE!!! Wrong Signature
            ////lamiaSomma = SecondaSomma;

            //Chiamami(lamiaSomma);
            //Chiamami(PrimaSomma);
            //// ERRORE
            //Chiamami(SecondaSomma);

            #endregion

            #region === Step 3 ===

            Func <int, int> lamdbaZero = x => 2 * x;

            Func <int, int> lamdbaZeroZero = x => {
                var result = 2 * x;
                return(result);
            };

            Func <int, int> lamdbaZeroZeroZero = Multiply;

            lamdbaZeroZero(45);

            var list = new List <int> {
                1, 2, 3, 4, 5, 6
            };
            //var results = Where(dataInt, x => x > 2);

            var results = list.Where(x => x > 2);

            Func <int, double, bool> lambdaOne = (x, y) => x > (int)y;

            #endregion

            #region === Step 4 ===

            List <EmployeeInt> employees = new List <EmployeeInt>
            {
                new EmployeeInt {
                    ID = 1, Name = "Roberto"
                },
                new EmployeeInt {
                    ID = 2, Name = "Alice"
                },
                new EmployeeInt {
                    ID = 3, Name = "Mauro"
                },
                new EmployeeInt {
                    ID = 4, Name = "Roberto"
                },
            };

            var result  = employees.Where("ID", "1");
            var result2 = employees.Where("Name", "Roberto");

            // value => value * value

            ParameterExpression y = Expression.Parameter(typeof(int), "value");
            var basettoni         = new ParameterExpression[] {
                y
            };

            Expression <Func <int, int> > squareExpression =
                Expression.Lambda <Func <int, int> >(
                    Expression.Multiply(y, y),
                    basettoni
                    );

            Expression <Func <int, int> > squareExpression2 = value => value * value;

            Func <int, int> funzione = squareExpression.Compile();
            Console.WriteLine(funzione(3));

            #endregion
        }