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()); 
        }
    }
天天来(http://www.daydaycome.com)】- 精选折扣商品,爆料精选,九块九白菜底价尽在天天来!是一个中立的,致力于帮助广大网友买到更有性价比网购产品的分享平台,每天为网友们提供最受追捧 最具性价比 最大幅降价潮流新品资讯。我们的信息大部分来自于网友爆料,如果您发现了优质的产品或好的价格,不妨给我们爆料(谢绝商家)
原文地址:https://www.cnblogs.com/Reborn/p/1714439.html