Aggregate operators of [LINQ standard query operator summary]

LINQ in C ා provides two operation modes: query expression and query operator. All query expressions are replaced by corresponding query operator classes. Query expression is a bit of "class" SQL. When writing SQL in the code, I always feel that it is not "elegant". Using query operators is "elegant". This series is about all LINQ The standard operators are a comprehensive summary, which is similar to what I summarized in the previous article RxJS operator There are a lot of similarities, even to think about, interested can click the link to view. As a whole, LINQ operators of C ා can be divided into 13 categories: aggregation, transformation, element operation, equality operation, generation, grouping, join, division, projection, quantity, filtering, set-based operator and sorting. It can be said that LINQ, lambda and generics have become the cornerstone of C language. How boring the code would be without these three features.

Aggregation Operators

All return values of an aggregate operation are just one value.

To demonstrate here, we define two arrays: an array representing string textSource and a numberSource array representing int type:

static string[] textSource = { "zero", "one", "two", "three","four","five","six","seven","eight","nine" };
static int[] numberSource = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

1.Sum is the sum of a sequence of built-in numeric types or elements converted from internal to internal numeric types by using delegates. For example:

//Sum the series of int type by direct accumulation
Console.WriteLine(numberSource.Sum()); //45
//Sum the sum of the sum of the squares of each element in the sequence of type int
Console.WriteLine(numberSource.Sum(s =>s*s));//285
//Add and sum the length of each string in the string array
Console.WriteLine(textSource.Sum(s=>s.Length));//40

2.Average is used to average the built-in numeric type or the sequence of elements converted from internal to internal numeric type by using delegation. For example:

//Direct averaging of int type sequences
Console.WriteLine(numberSource.Average()); //4.5
//Average each element in an int type sequence after square
Console.WriteLine(numberSource.Average(s => s * s));//28.5
//Average the length of each string in the string array
Console.WriteLine(textSource.Average(s => s.Length));//4

3. Count & LongCount two operators are used to find the number of elements in the set, only the return types are different. Count returns int type, and LongCount returns long type. For example:

//Find the number of elements in the array
Console.WriteLine(numberSource.Count()); //10
//Find the number of elements less than 6 in the array
Console.WriteLine(numberSource.Count(s => s<6));//6
//Find the number of elements with string length greater than 4
Console.WriteLine(textSource.Count(s => s.Length>4));//3

//Find the number of elements in the array                                          
Console.WriteLine(numberSource.LongCount()); //10
//Find the number of elements less than 6 in the array
Console.WriteLine(numberSource.LongCount(s => s < 6));//6
//Find the number of elements with string length greater than 4
Console.WriteLine(textSource.LongCount(s => s.Length > 4));//3

4. The Min & Max operators calculate the maximum or minimum value in the set, and can pass in a delegate to process the result. For example:

//Find the minimum value of an element in an int array
Console.WriteLine(numberSource.Min()); //0
//Find the minimum value of an element in an int array and compare it with 1
Console.WriteLine(numberSource.Min(s => s > 1));//False
//Find the length of the shortest string in the string array
Console.WriteLine(textSource.Min(s=>s.Length));//3

//Find the maximum value of an element in an int array
Console.WriteLine(numberSource.Max()); //9
//Find the maximum value of an element in an int array and compare it with 1
Console.WriteLine(numberSource.Max(s => s > 1));//True
//Find the length of the longest string in the string array
Console.WriteLine(textSource.Max(s => s.Length));//5

5.Aggregate is a very flexible operator. All aggregation operators can be replaced by this operator. This operator can also convert results to other types by delegation, which is widely used. Next, I'll use the aggregate operator to perform equivalent operations on the operators mentioned above.

//Sum the series of int type by direct accumulation
var sum = numberSource.Aggregate((current, item) => current + item);
Console.WriteLine(sum);//45

//Sum the sum of the sum of the squares of each element in the sequence of type int
Console.WriteLine(numberSource.Aggregate((current, item) => current + item * item));//285

//Add and sum the length of each string in the string array
Console.WriteLine(textSource.Aggregate(0, (current, item) => current + item.Length));//40

//Direct averaging of int type sequences
Console.WriteLine(numberSource.Aggregate(0, (current, item) => current + item, result => (float)result / numberSource.Length)); //4.5

//Average each element in an int type sequence after square
Console.WriteLine(numberSource.Aggregate(0, (current, item) => current + item * item, result => (float)result / numberSource.Length));//28.5

//Average the length of each string in the string array
Console.WriteLine(textSource.Aggregate(0, (current, item) => current + item.Length, result => (float)result / numberSource.Length));//4

//Find the number of elements in the array
Console.WriteLine(numberSource.Aggregate(0, (current, item) => current + 1)); //10

//Find the number of elements less than 6 in the array
Console.WriteLine(numberSource.Aggregate(0, (current, item) => item < 6 ? (current + 1) : current));//6

//Find the number of elements with string length greater than 4
Console.WriteLine(textSource.Aggregate(0, (current, item) => item.Length > 4 ? (current + 1) : current));//3

//Find the minimum value of an element in an int array
Console.WriteLine(numberSource.Aggregate(0, (current, item) => item <current ? current=item : current)); //0

//Find the minimum value of an element in an int array and compare it with 1
Console.WriteLine(numberSource.Aggregate(0, (current, item) => item < current ? current = item : current,resut=>resut>1));//False

//Find the length of the shortest string in the string array
Console.WriteLine(textSource.Aggregate(10, (current, item) => item.Length < current ? current = item.Length : current));//3

//Find the maximum value of an element in an int array
Console.WriteLine(numberSource.Aggregate(0, (current, item) => item > current ? current = item : current)); //9
//Find the maximum value of an element in an int array and compare it with 1
Console.WriteLine(numberSource.Aggregate(0, (current, item) => item > current ? current = item : current, resut => resut > 1));//True
//Find the length of the longest string in the string array
Console.WriteLine(textSource.Aggregate(0, (current, item) => item.Length > current ? current = item.Length : current));//5

//Sum every item of numberSource on the basis of 100
var sumWithSeed = numberSource.Aggregate(100, (current, item) => current + item);
Console.WriteLine(sumWithSeed);//145

//Sum every item of numberSource on the basis of 100, and square the result
var sumWithSeedAndResultSquare = numberSource.Aggregate(100, (current, item) => current + item, result => result * result);
Console.WriteLine(sumWithSeedAndResultSquare);//21025

//On the basis of 100, sum every term of numberSource after the square, and open the square root of the result
var sumWithSeedItemSquareAndResultSqrt = numberSource.Aggregate(100, (current, item) => current + item * item, result => Math.Sqrt(result));
Console.WriteLine(sumWithSeedItemSquareAndResultSqrt);//19.621416870348583

//String splicing
var concatArray = textSource.Aggregate((current, item) => current + "," + item);
Console.WriteLine(concatArray);//zero,one,two,three,four,five,six,seven,eight,nine

//Concatenated string disease with the length of each character
var concatArrayWithLength = textSource.Aggregate("",(current, item) => current + "," + item+":"+item.Length,resut=>resut.TrimStart(','));
Console.WriteLine(concatArrayWithLength);//zero:4,one:3,two:3,three:5,four:4,five:4,six:3,seven:5,eight:5,nine:4

All other aggregation operators can be represented as calls to Aggregate, although this is relatively cumbersome. The basic idea is that there is always a "current result" that starts with the initial element. The aggregation delegate is applied to each element of the input sequence; the delegate takes the current result and the input element and generates the next result. As a final optional step, the transformation is applied to the Aggregate result and converted to the return value of the method. If necessary, this transformation can produce different data types.

Aggregate operators are very simple, they are executed immediately, as long as the code is called, the result will be returned immediately.

Keywords: C# less SQL Lambda C

Added by nloding on Sat, 30 May 2020 17:01:17 +0300