LINQ learning notes (8) standard query operators (bottom)

Source: Internet
Author: User

Next, I will summarize the content of the standard query operator (lower) in the morning. The main content is about the parameter knowledge of the delegate as a standard query operator.

First, we need to know what is the role of using generic delegation as a parameter? Generic delegation is used to provide operators with user-defined code.

The predefined delegate type of LINQ:

LINQ defines a set of five generic delegation types used with standard query operators. They are func delegates. The following code is used:

Public Delegate tr func <tr> (); // 1st, no parameter.
Public Delegate tr func <t1, tr> (T1 A1); // type 2nd. One parameter is accepted. tr indicates the return value.
Public Delegate tr 'func <T1, T2, tr> (T1 A1, T2 A2); // two parameters are accepted.
Public Delegate tr 'func <T1, T2, T3, tr> (T1 A1, T2 A2, T3 A3); // you can specify three parameters.
Public Delegate tr 'func <T1, T2, T3, T4, tr> (T1 A1, T2 A2, T3 A3, T4 A4); // There are 5th parameters, and four parameters are accepted.

In addition, we need to note that:

1. The delegate object we use as the real parameter must be one of these five types.

2. tr indicates the return value and is always the last in the type parameter list.

After learning about this, let's look back at the count statement. We can find that the second parameter must be a delegate object, it accepts a single T-type parameter as a method parameter and returns a bool-type value. For example:

Public static int count <t> (this ienumerable <t> source, func <t, bool> predicate); // where T indicates the parameter type and bool indicates the return type.
Use func to delegate:

We have a preliminary understanding of the signature of the count operator and the generic delegate parameters in LINQ. Below we will use an example to better understand the application of the generic delegate in the standard query operator.

1 class Program
2 {
3 static bool isodd (int x) // method used to create a generic delegate object
4 {
5 return X % 2 = 1;
6}
7 static void main (string [] ARGs)
8 {
9 int [] intarray = {3, 4, 5, 6, 7, 9 };
10
11 func <int, bool> MYDEL = new func <int, bool> (isodd); // create a generic delegate object and use the isodd () method to initialize it.
12 var countodd = intarray. Count (MYDEL); // use the delegate.
13
14 console. writeline ("count of odd numbers: {0}", countodd );
15 console. readkey ();
16}
17 /*
18 program output: Count of odd numbers: 4
19 */
20}
Use the anonymous method to simplify the Code:

The Code is as follows:

1 class Program
2 {
3 static void main (string [] ARGs)
4 {
5 Int [] intarray = {3, 4, 5, 6, 7, 9 };
6
7 func <int, bool> MYDEL = delegate (int x) // use the abbreviated anonymous method
8 {
9 return X % 2 = 1;
10 };
11 var countodd = intarray. Count (MYDEL); // use the delegate.
12
13 console. writeline ("count of odd numbers: {0}", countodd );
14 console. readkey ();
15}
16 /*
17 program output: Count of odd numbers: 4
18 */
19}
Use lambda expressions to simplify the Code:

We can continue to use lambda expressions instead of anonymous methods to simplify the code, as shown below:

1 class Program
2 {
3 static void main (string [] ARGs)
4 {
5 Int [] intarray = {3, 4, 5, 6, 7, 9 };
6
7 var countodd = intarray. Count (x => X % 2 = 1); // use the lambda expression
8
9 console. writeline ("count of odd numbers: {0}", countodd );
10 console. readkey ();
11}
12 /*
13 program output: Count of odd numbers: 4
14 */
15}

Using lambda expressions is also the most common method in practice, so we must be very skilled. At this point, the preparation of the LINQ query is almost finished. I will start from the next article to learn the actual usage of the LINQ query. I hope you will continue to pay attention to it. Thank you!

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.