7. 聚合操作符—【LINQ标准查询操作符】

public class Aggregate_LINQ
    {
        static string ContextString = System.Configuration.ConfigurationSettings.AppSettings["ContextString"].ToString();
        static DataContext context = new DataContext(ContextString);
        static Table<SalesOrderDetail> orderdetail = context.GetTable<SalesOrderDetail>();

        public static void Aggregate_Print()
        {
            string names = "Carla,Ronald,James,François,Amy";
            string[] newName = names.Split(',');
            string str = newName.Aggregate((c, d) => c + " : " + d);            
           
            Console.WriteLine(str);
        }

        public static void Average_Print()
        {
            //List<int> quantity = new List<int> { 9, 8, 120, 1, 2 };
            //double average = quantity.Average();
            //Console.WriteLine(average.ToString());

            var query = from o in orderdetail
                        where o.SalesOrderID == 43662
                        select o.UnitPrice;
            Console.WriteLine(query.Average());

            var query2 = from o in orderdetail
                         where o.SalesOrderID == 43662
                         select o;
            Console.WriteLine(query2.Average(o => o.UnitPrice));
        }

        public static void Count_Print()
        {
            List<int> quantity = new List<int> { 23, 234, 54, 56, 1, 642 };
            Console.WriteLine(quantity.Count());

            var query = from o in orderdetail
                        where o.SalesOrderID == 43662
                        select o;
            Console.WriteLine(query.Count(o => o.UnitPrice < 400));
        }

        public static void LongCount_Print()
        {
            List<Int64> quantity = new List<Int64> { 99, 48, 120, 73, 101, 81, 56 };
            Int64 cnt = quantity.LongCount();
            Console.WriteLine(cnt.ToString());

            var query = from o in orderdetail
                        where o.SalesOrderID == 43662
                        select o;
            Console.WriteLine(query.LongCount(o => o.UnitPrice < 400));            
        }

        public static void Max_Print()
        {
            List<int> quantity = new List<int> { 99, 2, 21, 45, 93, 202, 22, 90 };
            Console.WriteLine(quantity.Max());            
        }

        public static void Min_Print()
        {
            List<int> quantity = new List<int> { 99, 2, 21, 45, 93, 202, 22, 90 };
            Console.WriteLine(quantity.Min()); 
        }

        public static void Sum_Print()
        {
            List<int> quantity = new List<int> { 99, 2, 21, 45, 93, 202, 22, 90 };
            Console.WriteLine(quantity.Sum()); 
        }
    }
posted @ 2010-04-17 21:56  CityWalker  阅读(295)  评论(0编辑  收藏  举报
天天来