首页 > 学院 > 开发设计 > 正文

Linq中的Aggragate的使用

2019-11-06 06:53:33
字体:
来源:转载
供稿:网友
Aggraget操作符是从某个特定序列或集合中收集值。主要用于简单的累加、阶乘、反转单词序列。我们先一个一个的演示。

演示1:用于累加:

这是用Aggragate方法的案例:

  public static void AggragateAccumulation()        {            int[] array = { 1, 2, 3, 4, 5, 6 };            /*             * 1+2=3;             * 3+3=6;             * 6+4=10;             * 10+5=15;             * 15+6=21;             */            int result = array.Aggregate((a, b) => a + b);            Console.WriteLine("累加的结果是:{0}",result);                    }

用普通方法累加:

public static void Accumulation()        {            int[] array = { 1, 2, 3, 4, 5, 6 };            int result = 0;            foreach (int i in array)                result += i;            Console.WriteLine("累加的结果是:" + result);        }

演示2:IEnumeraber<int>用于阶乘:

public static void AggregateFactorial()        {            var numbers = IntArray(6);            //1*2=2;            //2*3=6;            //6*4=24;            //24*5=120;            //120*6=720;            var result = numbers                .Select(n => n)                .Aggregate((total, next) =>                {                    return total * next;                });                       Console.WriteLine("6的阶乘是:{0}", result);        }PRivate static IEnumerable<int> IntArray(int max)        {            List<int> result = new List<int>();            for (int i = 0; i < max; i++)                result.Add(i + 1);            return result;        }

用普通方法计算阶乘:

 public static void Factorial()        {            var ints = IntArray(6);            int result = 1;//注意这是初始值为1            foreach (var i in ints)                result *= i;            Console.WriteLine("6的阶乘的是:{0}", result);        }

int[]数组用于阶乘:

public static void AggregateArrFactorial()        {            int[] array = { 1, 2, 3, 4, 5, 6 };            int result = array.Aggregate((a, b) => a * b);            Console.WriteLine("6的阶乘是:{0}",result);        }        public static void ArrayFactorial()        {            int[] array = { 1, 2, 3, 4, 5, 6 };            int result = 1;            foreach (var i in array)                result *= i;            Console.WriteLine("6的阶乘是:{0}", result);        }

 演示3:string[]用于字符串数组的反转

public static void AggragateReverse()        {            string city = "GuangZhou,ZhuHai,ShenZhen,ChangSha,ChangDe";            string[] cities = city.Split(',');            string newCity = cities                .Aggregate((currentCity, nextCity) => nextCity + " " + currentCity);            string aa="aaa";                      Console.WriteLine("反转后的城市为:{0}", newCity);        }

 用普通方法计算字符串数组反转:

public static void ReverseCity()        {            string city = "GuangZhou,ZhuHai,ShenZhen,ChangSha,ChangDe";            string[] cities = city.Split(',');            string newCity = "";            for (int i = cities.Length - 1; i >= 0; i--)                newCity += cities[i] + " ";            Console.WriteLine("反转后的城市为:{0}", newCity);        }

 

 演示4:应用累加器函数和结果选择器

  public static void AggragateSelector()        {            string[] fruits = { "apple", "mango", "pear", "avocado", "banana", "watermelon", "passionfruit", "grape" };            string lonestFruit = fruits                .Aggregate("avocado",                (longest, next) => next.Length > longest.Length ? next : longest,                fruit => fruit.ToUpper());            Console.WriteLine("名字最长的水果是:{0}", lonestFruit);        }

用普通方法演示上例,代码如下:

public static void LongestFruit()        {            string[] fruits = { "apple", "mango", "pear", "avocado", "banana", "watermelon", "grape", "passionfruit"};            string longestFruit = "";            for (int i = 0; i < fruits.Length; i++)            {                for (int j = i + 1; j < fruits.Length; j++)                {                    int fruitLength = fruits[i].Length;                    int nextFruitLength = fruits[j].Length;                    if (nextFruitLength > fruitLength)                        longestFruit = fruits[j];                    else                        longestFruit = fruits[i];                }            }            Console.WriteLine("名字最长的水果是:{0}", longestFruit.ToUpper());        }

 各位同仁,如果有更好的方法,大家可以进行讨论,这里没有给出各个两种方式的效率的对比。大家可以自行比较。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表