LINQ standard Query Operators (iii)--aggregate, Average, Distinct, Except, Intersect, Union, Empty, DefaultIfEmpty, Range, Repeat

Source: Internet
Author: User

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
  1. using (NorthwindDataContext db = new NorthwindDataContext ())
  2. {
  3. Db. Log = Console.Out; //Outputs the generated T-SQL statements to the console
  4. //Method Syntax
  5. var q =
  6. Db. Categories
  7. . Select (c = c.categoryname)
  8. . ToArray ()
  9. .  Aggregate (current, next) = String.Format ("{0}, {1}", current, next));
  10. Console.WriteLine (q);
  11. }

If you are confused about this process, please refer to the following code:

[CSharp]View Plaincopy
    1. var query =
    2. Db. Categories
    3. . Select (c = c.categoryname)
    4. . ToArray ();
    5. string r = String.Empty;
    6. foreach (var item in query)
    7. {
    8. r + = ",";
    9. R + = Item;
    10. }
    11. r = r.substring (1); //Removal of the first comma
    12. 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
    1. var q = enumerable.empty<int> ();
    2. Console.WriteLine (q = = null);
    3. 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
  1. using (NorthwindDataContext db = new NorthwindDataContext ())
  2. {
  3. Db. Log = Console.Out; //Outputs the generated T-SQL statements to the console
  4. //Method Syntax
  5. var q =
  6. Enumerable.defaultifempty
  7. (
  8. Db. Employees
  9. . Where (E = E.firstname.startswith ("Aaf")) //change the conditions here to obtain a different set
  10. , new Employees () {FirstName = "Sunny d.d"}
  11. );
  12. Console.WriteLine (Q.count ());
  13. foreach (var item in q)
  14. {
  15. Console.WriteLine (item. FirstName);
  16. }
  17. }

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
    1. var q =
    2. Enumerable.range (0, 10);
    3. foreach (var item in q)
    4. {
    5. Console.WriteLine (item);
    6. }

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
    1. var q =
    2. Enumerable.repeat (0, 10);
    3. foreach (var item in q)
    4. {
    5. Console.WriteLine (item);
    6. }

LINQ standard Query Operators (iii)--aggregate, Average, Distinct, Except, Intersect, Union, Empty, DefaultIfEmpty, Range, Repeat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.