コード例 #1
0
        /*public static IEnumerable<IGrouping<K, T>> GroupBy<T, K, E>(
         * 	this IEnumerable<T> source,
         * 	Func<T, K> keySelector,
         * 	Func<T, E> elementSelector.
         * 	IEqualityComparer<K> comparer);
         * */
        void GroupByOperator4th()
        {
            MyFounderNumberComparer comp = new MyFounderNumberComparer ();
            EmployeeOptionEntry[] emOptions = EmployeeOptionEntry.GetEmployeeOptionEntries ();

            IEnumerable<IGrouping<int, DateTime>> opts = emOptions
                .GroupBy (key => key.id, element => element.dateAwarded, comp);

            foreach (IGrouping<int, DateTime> keyGroup in opts) {
                Console.WriteLine("Option dates for: " + (comp.isFounder(keyGroup.Key) ? "founder" : "nonfounder"));

                foreach(DateTime date in keyGroup)
                {
                    Console.WriteLine(date.ToShortDateString());
                }
            }
        }
コード例 #2
0
        /*public static IEnumerable<IGrouping<K, T>> GroupBy<T, K>(
         * 	this IEnumerable<T> source,
         * 	Func<T, K> keySelector,
         * 	IEqualityComparer<K> comparer);
         * */
        void GroupByOperator2nd()
        {
            MyFounderNumberComparer comp = new MyFounderNumberComparer();

            EmployeeOptionEntry[] emOptions = EmployeeOptionEntry.GetEmployeeOptionEntries();

            IEnumerable<IGrouping<int, EmployeeOptionEntry>> opts = emOptions
                .GroupBy(o => o.id, comp);

            //First enumerate trough the sequence of IGroupings.
            foreach(IGrouping<int, EmployeeOptionEntry> keyGroup in opts)
            {
                Console.WriteLine("Option records for: " + (comp.isFounder(keyGroup.Key) ? "founder" :"nonfounder"));

                //now enumerate trough the grouping's sequence of EmployeeOptionEntry elements.
                foreach(EmployeeOptionEntry element in keyGroup)
                {
                    Console.WriteLine("id={0} : optionsCount= {1} : dateAwarded={2:d}",
                                      element.id, element.optionsCount, element.dateAwarded);
                }
            }
        }