Ejemplo n.º 1
0
        public static void Demo2()
        {
            Employee[]            employees  = Employee.GetEmployeesArray();
            EmployeeOptionEntry[] empOptions = EmployeeOptionEntry.GetEmployeeOptionEntries();

            var employeeOptions = employees
                                  .SelectMany(e => empOptions
                                              .Where(eo => eo.id == e.id)
                                              .Select(eo => new
            {
                id           = eo.id,
                optionsCount = eo.optionsCount
            }));

            foreach (var item in employeeOptions)
            {
                Console.WriteLine(item);
            }

            /*
             *  { id = 1, optionsCount = 2 }
             *  { id = 2, optionsCount = 10000 }
             *  { id = 2, optionsCount = 10000 }
             *  { id = 2, optionsCount = 10000 }
             *  { id = 3, optionsCount = 5000 }
             *  { id = 3, optionsCount = 7500 }
             *  { id = 3, optionsCount = 7500 }
             *  { id = 4, optionsCount = 1500 }
             *  { id = 101, optionsCount = 2 }
             */
        }
        public static void EmployeeOptions()
        {
            EmployeeOptionEntry[] EmpOptions = EmployeeOptionEntry.GetEmployeeOptionEntries();
            var Employees = Initial_Get_Employee_Array_List.shared_Employee_Classes;

            var employeeOptions = Employees
                                  .SelectMany(e => EmpOptions
                                              .Where(eo => eo.id == e.id)
                                              .Select(eo => new
            {
                Id           = eo.id,
                OptionsCount = eo.optionsCount
            }));

            foreach (var items in employeeOptions)
            {
                Console.WriteLine(items);//OutPut:  { id = 1, optionsCount = 2 } ... id = 101
            }
            //======================================Sumary====================================
            //This looks like a lot but lets step through it. In the example every employee in the
            //Employee list is passed into he lambda expression which is then passed into the
            //selectmany Operator. The lambda expression will then get every entry of employee
            //optionEntry elements whos ID matches the ID of the current employee passed into it
            //After that the lambda creates a new anonymous object for each matching ID
            //TLDR: Takes every matching id from the two arrays and adds them into their own object.
            //================================================================================
        }
Ejemplo n.º 3
0
            public static EmployeeOptionEntry[] GetEmployeeOptionEntries()
            {
                EmployeeOptionEntry[] empOptions = new EmployeeOptionEntry[] {
                    new EmployeeOptionEntry {
                        id           = 1,
                        optionsCount = 2,
                        dateAwarded  = DateTime.Parse("1999/12/31")
                    },
                    new EmployeeOptionEntry {
                        id           = 2,
                        optionsCount = 10000,
                        dateAwarded  = DateTime.Parse("1992/06/30")
                    },
                    new EmployeeOptionEntry {
                        id           = 2,
                        optionsCount = 10000,
                        dateAwarded  = DateTime.Parse("1994/01/01")
                    },
                    new EmployeeOptionEntry {
                        id           = 3,
                        optionsCount = 5000,
                        dateAwarded  = DateTime.Parse("1997/09/30")
                    },
                    new EmployeeOptionEntry {
                        id           = 2,
                        optionsCount = 10000,
                        dateAwarded  = DateTime.Parse("2003/04/01")
                    },
                    new EmployeeOptionEntry {
                        id           = 3,
                        optionsCount = 7500,
                        dateAwarded  = DateTime.Parse("1998/09/30")
                    },
                    new EmployeeOptionEntry {
                        id           = 3,
                        optionsCount = 7500,
                        dateAwarded  = DateTime.Parse("1998/09/30")
                    },
                    new EmployeeOptionEntry {
                        id           = 4,
                        optionsCount = 1500,
                        dateAwarded  = DateTime.Parse("1997/12/31")
                    },
                    new EmployeeOptionEntry {
                        id           = 101,
                        optionsCount = 2,
                        dateAwarded  = DateTime.Parse("1998/12/31")
                    }
                };

                return(empOptions);
            }
Ejemplo n.º 4
0
        //====================================Join====================================
        //The join operator performs an inner equiijoin on 2 sequences, comparing the
        //extracted keys from the elements.
        //===================================Prototype================================
        //
        //          public static IEnumerable<V> Join<T, U, K, V>(
        //              this IEnumerable<T> outer,
        //               IEnumerable<U> inner,
        //               Func<T, K> outerKeySelector,
        //              Func<U, K> innerKeySelector,
        //              Func<T, U, V> resultSelector);
        //
        //===================================Sumary===================================
        //The first method is called outer. Since this is an extension method, the obj
        //we are calling this on will be considered the outer. The operator will return
        //an object when enumerated will first enumerate the inner sequence(U). This calls
        //the inner keyselector once for each element in the sequence. This stores the
        //elements referenced with a specific key into a hash table. After the same thing
        // will occur for the outer sequence(T). for each matching inner and outer element
        //pair the resultSelector method (v) will be called.
        //=============================================================================
        public static void JoinEmployees()
        {
            var joinByIds = Initial_Get_Employee_Array_List.shared_Employee_Classes
                            .Join(
                EmployeeOptionEntry.GetEmployeeOptionEntries(), //Inner Sequence
                e => e.id,                                      //outer KeySelector
                i => i.id,                                      //Inner KeySelector
                (e, i) => new                                   //Result Selector
            {
                id      = e.id,
                name    = string.Format($"{e.firstName} {e.lastName}"),
                options = i.optionsCount
            });

            foreach (var item in joinByIds)
            {
                Console.WriteLine(item);
            }
        }