link中aggregate有什么用,和selectmany的区别是什么?

link中aggregate有什么用,和selectmany的区别是什么?

我把concat也实现了,请看

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] arr = new int[][]
            {
                new int[] { 1, 2, 3 },
                new int[] { 4, 5 },
                new int[] { 6, 7 }
            };
            int[] query1 = arr.MySelectMany(x => x).ToArray();
            foreach (var item in query1)
                Console.WriteLine(item);
            Console.WriteLine();
            int[] query2 = arr.MyAggregate(new int[] { }, (pre, curr) => pre.MyConcat(curr));
            foreach (var item in query2)
                Console.WriteLine(item);
        }
    }

    static class MyLinq
    {
        public static int[] MyConcat(this int[] data1, int[] data2)
        { 
            int[] result = new int[data1.Length + data2.Length];
            for (int i = 0; i < data1.Length; i++)
            {
                result[i] = data1[i];
            }
            for (int i = data1.Length; i < data1.Length + data2.Length; i++)
            {
                result[i] = data2[i - data1.Length];
            }
            return result;
        }

        public static IEnumerable<int> MySelectMany(this int[][] data, Func<int[], IEnumerable<int>> selector)
        {
            foreach (int[] item in data)
                foreach (int i in selector(item))
                    yield return i;
        }

        public static int[] MyAggregate(this int[][] data, int[] seed, Func<int[], int[], int[]> func)
        {
            int[] result = seed;
            foreach (int[] item in data)
                result = func(result, item);
            return result;
        }
    }
}

看看例子你就明白了: https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

//此处相当于  1.7x2.3x1.9x4.1x2.9
public void Linq92() 
{ 
    double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; 

    double product = doubles.Aggregate((runningProduct, nextFactor) => runningProduct * nextFactor); 

    Console.WriteLine("Total product of all numbers: {0}", product); 
}

   int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; 
    int[] numbersB = { 1, 3, 5, 7, 8 }; 

    var pairs = 
        from a in numbersA 
        from b in numbersB 
        where a < b 
        select new { a, b }; 

    Console.WriteLine("Pairs where a < b:"); 
    foreach (var pair in pairs) 
    { 
        Console.WriteLine("{0} is less than {1}", pair.a, pair.b); 
    } 

再跟你说一遍, 不是link ,是 LINQ

aggregate和selectmany根本是完全不同的两个东西,只是都能解决你问的如何将交错数组变成一维数组的问题而已。

督察说的很对,是linq不是link

为了说明它们有什么不同,我把它们的实现写给你

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] arr = new int[][]
            {
                new int[] { 1, 2, 3 },
                new int[] { 4, 5 },
                new int[] { 6, 7 }
            };
            int[] query1 = arr.MySelectMany(x => x).ToArray();
            foreach (var item in query1)
                Console.WriteLine(item);
            Console.WriteLine();
            int[] query2 = arr.MyAggregate(new int[] { }, (pre, curr) => pre.Concat(curr).ToArray());
            foreach (var item in query2)
                Console.WriteLine(item);
        }
    }

    static class MyLinq
    {
        public static IEnumerable<int> MySelectMany(this int[][] data, Func<int[], IEnumerable<int>> selector)
        {
            foreach (int[] item in data)
                foreach (int i in selector(item))
                    yield return i;
        }

        public static int[] MyAggregate(this int[][] data, int[] seed, Func<int[], int[], int[]> func)
        {
            int[] result = seed;
            foreach (int[] item in data)
                result = func(result, item);
            return result;
        }
    }
}

你可以看到,selectmany其实是两重循环。而aggregate是迭代累加。

aggregate做了一重循环,而concat其实实现了另一重。而且很明显,aggregate实现的算法,需要不断分配和复制数组,所以虽然实现了和selectmany一样的功能,但是效率要低一些。

这是结果,都是一样的。

 1
2
3
4
5
6
7

1
2
3
4
5
6
7
请按任意键继续. . .