Linq-Advanced Query method

Source: Internet
Author: User

For advanced query methods, mainly divided into five categories, details:


"Distinct": Avoid duplicate values, that is, if there are two number 2, then use the collection class can appear single

First, let's write an array that's handy for example:

Array data persons int[] Arr = {25, 36, 25, 2, 3, 9, 55, 581, 21, 15, 44, 78, 96};

    • Aggregation classes

            Console.WriteLine ("Arr Max:" + Arr.max ());            Console.WriteLine ("The Minimum value of ARR:" + arr.min ());            Console.WriteLine ("The average of Arr:" + arr.average ());            Console.WriteLine ("Number of array elements in ARR:" + arr.count ());            Console.WriteLine ("The Sum of ARR:" + arr.sum ());
simple to understand is to use the array of ARR properties to display the desired effect:

    • Sort class, Keyword ThenBy

            var result = Arr.orderby (p = = p.tostring (). Substring (0, 1));  According to the first number from small to large sort            var result = Arr.orderby (p = = p.tostring (). Substring (0, 1)). ThenBy (p = p);  Two orders            //descending permutation            var t = Arr.orderby (P = = p.tostring (). Substring (0, 1));                 var result = from P in T is the P descending select p;
three methods, the first display effect is sorted by the first number, but this method generally does not meet the requirements, and second, on the basis of the first order from small to large, and the third is in descending order.

The second kind of display effect:

    • Partition class:

Take a skip,skip and skip the idea

            var result = Arr.skip (2). Take (3);        Skip two and fetch three values             var result = Arr.skip (3);                Skips the value of three, after the remainder of the            var result = arr.skipwhile (P = P >1);   Remove all values after the remaining greater than or equal to p, such as p>1, where all values are satisfied, so the output value is 0            var result = arr.takewhile (P = p > 4);  Remove all values that meet the criteria for the first time, and stop execution when a non-conforming condition is encountered
in the upper array, because each value is greater than 1, the value of the third output is null, and the last one to 2 does not satisfy the condition and stops execution, so the output is:

    • Generating classes

For the last generated class, it is not the previous extension method, but the normal static method

Range can only produce integer sequences, repeat can only produce generic sequences

Example:

var result = System.Linq.Enumerable.Repeat (DateTime.Now, ten);    Show current date, and number of times
The five major categories mainly include LINQQ's various common query methods, others continue to learn in practice.

Linq-Advanced Query method

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.