This article from: http://blog.csdn.net/xuejianwu/article/details/6931903
Vii. Aggregation Operators
The aggregate function performs a specific calculation on the sequence and returns a single value, such as calculating the average of a given sequence, the maximum value, and so on. There are 7 types of LINQ aggregation Query operators: Aggregate, Average, Count, LongCount, Max, Min, and sum.
1. Aggregate
The aggregate operator performs a custom aggregation operation on the collection value. For example, you need to list all product category lists, with comma connections between each category name. The following code demonstrates this process:
[CSharp]View Plaincopy
- using (NorthwindDataContext db = new NorthwindDataContext ())
- {
- Db. Log = Console.Out; //Outputs the generated T-SQL statements to the console
- //Method Syntax
- var q =
- Db. Categories
- . Select (c = c.categoryname)
- . ToArray ()
- . Aggregate (current, next) = String.Format ("{0}, {1}", current, next));
- Console.WriteLine (q);
- }
If you are confused about this process, please refer to the following code:
[CSharp]View Plaincopy
- var query =
- Db. Categories
- . Select (c = c.categoryname)
- . ToArray ();
- string r = String.Empty;
- foreach (var item in query)
- {
- r + = ",";
- R + = Item;
- }
- r = r.substring (1); //Removal of the first comma
- Console.WriteLine (R);
2. Average
The average of the elements in the collection, the return value type double
3. Count
Find the number of elements in the collection, return value type Int32
4. LongCount
Find the number of elements in the collection, return value type Int64
5. Max
To find the maximum value of an element in a collection
6. Min
To find the minimum value of an element in a collection
7. Sum
The and of the elements in the collection.
Eight, set operator
The set operators in LINQ are query operations that produce result sets based on the existence of equivalent elements in the same or different collections (or sets), with a total of 4 types:
method name |
description |
Di Stinct |
Removes duplicate values from the collection. |
|
Ex cept |
Returns the difference set, which is an element located in one collection but not in another. |
|
in Tersect |
Returns the intersection, which is an element that appears in two collections at the same time. |
|
Un Ion |
Returns the set, which is the only element that is located in any of the two collections. |
|
All are used in the "collection 1. Method Name (set 2)", and the return value is a collection of the result of the operation, which is not demonstrated here.
Nine, generate operator
Build refers to the creation of a new sequence of values.
1. Empty
The empty operator returns a blank collection of the specified type. The empty space here is not NULL, but a collection of 0 elements. The following example shows how to create an empty collection of type ienumerable<int>:
[CSharp]View Plaincopy
- var q = enumerable.empty<int> ();
- Console.WriteLine (q = = null);
- Console.WriteLine (Q.count ());
2. DefaultIfEmpty
DefaultIfEmpty replaces the empty collection with a single instance collection with default values. The collection executed by this method will have at least one element, because the DefaultIfEmpty method requires two parameters, the first parameter is a generic collection, the second parameter is a single element of the corresponding type, and if the first argument does not contain any elements, it returns the single element specified by the second argument. If you use the overloaded method of the DefaultIfEmpty method defaultifempty<t> (ienumerable<t> array), if the specified array set is empty, then a type of T is returned, A single object with a value of NULL. The following code demonstrates this process:
[CSharp]View Plaincopy
- using (NorthwindDataContext db = new NorthwindDataContext ())
- {
- Db. Log = Console.Out; //Outputs the generated T-SQL statements to the console
- //Method Syntax
- var q =
- Enumerable.defaultifempty
- (
- Db. Employees
- . Where (E = E.firstname.startswith ("Aaf")) //change the conditions here to obtain a different set
- , new Employees () {FirstName = "Sunny d.d"}
- );
- Console.WriteLine (Q.count ());
- foreach (var item in q)
- {
- Console.WriteLine (item. FirstName);
- }
- }
3. Range
The range operator is used to generate a sequence of integers within a specified range. It requires two parameters, the first parameter is the integer value at the beginning of the sequence, and the second argument is the number of integers in the sequence. The following example demonstrates using the range operator to generate a sequence of integers from 0 to 9:
[CSharp]View Plaincopy
- var q =
- Enumerable.range (0, 10);
- foreach (var item in q)
- {
- Console.WriteLine (item);
- }
4. Repeat
The repeat operator is used to generate a collection that contains a repeating value. It requires two parameters, the first parameter is an element of any type, and the second parameter is the number of this element contained in the generated sequence. The following example shows the use of repeat to generate a sequence containing 10 0:
[CSharp]View Plaincopy
- var q =
- Enumerable.repeat (0, 10);
- foreach (var item in q)
- {
- Console.WriteLine (item);
- }
LINQ standard Query Operators (iii)--aggregate, Average, Distinct, Except, Intersect, Union, Empty, DefaultIfEmpty, Range, Repeat