【笔记】【LINQ编程技术内幕】第八章 执行聚合运算

    技术2022-07-11  93

    聚合

    聚合运算就是从一组值中计算出一个值。Aggregate拓展方法允许实现自定义的聚合运算。下例共使用了三个不同的序列,然后将阿门聚合到一个序列中。

    int[] numbers1 = new int[] { 1, 2, 3 }; int[] numbers2 = new int[] { 4, 5, 6 }; int[] numbers3 = new int[] { 7, 8, 9 }; List<int[]> all = new List<int[]>{numbers1, numbers2, numbers3}; all.Dump(); // 最简单的调用方式 var allNumbers2 = all.Aggregate((current,next) => current.Union(next).ToArray<int>()); // 带初始值的调用 var allNumbers1 = all.Aggregate(Enumerable.Empty<int>(), (current,next) => current.Union(next)); // 静态函数 var allNumbers3 = Enumerable.Aggregate(all, Enumerable.Empty<int>(),(current, next) => current.Union(next)); Array.ForEach(allNumbers1.ToArray(), n => Console.WriteLine(n));

    求集合平均值

    平均值

    int[] numbers = new int[] { 13, 24, 5, 6, 78, 23 }; Console.WriteLine(numbers.Average());

    指定文件夹下文件的平均大小

    string[] files = Directory.GetFiles("C:\\Users\\Administrator"); double averageFileSize = (from file in files select (new FileInfo(file)).Length).Average(); averageFileSize = Math.Round(averageFileSize / 1000000, 2); Console.WriteLine("Average file size (in C:\\Users\\Administrator) {0} Mbytes", averageFileSize);

    求取一个文件中所有单词的平均长度 将以下内容拷贝并保存到WalrusAndCarpenter.txt文件中 The time has come the walrus said, to talk of many things. Of shoes–of ships–of sealing wax–of cabbages and kinds And why the sea is boiling hot and whether pigs have wings

    string[] lines = File.ReadAllLines("..\\..\\WalrusAndCarpenter.txt"); // sanity check Array.ForEach(lines, line => Console.WriteLine(line)); Func<string, string[]> Split = str => str.Split(new char[] { '-', ',', ' ', '.' }, StringSplitOptions.RemoveEmptyEntries); List<string> all = new List<string>(); //split out words Array.ForEach(lines, line => all.AddRange(Split(line))); // sanity check Array.ForEach(all.ToArray(), word => Console.WriteLine(word)); //send result Console.WriteLine("Average word length {0}", Math.Round(all.Average(w => w.Length), 2));

    最大最小元素

    var numbers = new float[] { 1.0f, 3.5f, -7.8f, 10f }; Console.WriteLine("Min: {0}", numbers.Min()); Console.WriteLine("Max: {0}", numbers.Max());

    求和

    保存以下数据到逗号文件(.csv)

    3,5,7,9,13 1,2,3,4,5 6,7,8,9,10 123,12,1,0,8 -3,-23,22,44, 7

    string[] lines = File.ReadAllLines("..\\..\\csvData.txt"); // make sure we have some data foreach (string s in lines) Console.WriteLine("{0}", s); var results = from line in lines let values = line.Split(',') let y = values select (from str in y select Convert.ToInt32(str)); Console.WriteLine(); for (int i = 0; i < results.Count(); i++) { Console.WriteLine("Total Column {0}: {1}", i + 1, results.Sum(o => o.ElementAt(i))); }

    中位数:实现一个自定义聚合运算

    class Program { static void Main(string[] args) { var numbers = new int[] { 3, 4, 5, 6, 7, 8, 9, 10 }; Console.Write(numbers.Median()); Console.ReadLine(); } } public static class MyAggregate { public static T Median<T>(this IEnumerable<T> source) { var ordered = from s in source orderby s select s; return source.ToArray()[ordered.Count() / 2]; } }
    Processed: 0.012, SQL: 9