C #4.0 graphic tutorial-Chapter 1 Introduction to LINQ (2)

Source: Internet
Author: User

21.5.7 anonymous type in Query

21.5.8 group clause

21.5.9 query continuation

21.6 standard query Operators

21.6.3 delegate as parameter

21.6.4 pre-defined delegation type of LINQ

21.6.5 example of using delegate Parameters

21.6.6 example of using Lambda expression Parameters

 

21.5.7 anonymous type in Query

Select New {S. lastname, S. firstname, S. Major };

 

 

21.5.8 group clause

The Group clause groups select objects according to some standards.

GroupStudentByStudent. Major;

If items are included in the query results, they can be grouped based on the value of a field. The item used as a grouping sentence is called a key ).

 Static  Void  Main (){  VaR Students = New [] //  Array of objects of an anonymous type  {  New {Lname = "  Jones  " , Fname = "  Mary  " , Age =19 , Major = "  History  "  },  New {Lname = "  Smith  " , Fname = "  Bob  " , Age = 20 , Major = "  Compsci  " },  New {Lname = "  Fleming  " , Fname = "  Carol  " , Age = 21 , Major = "  History  "  }};  VaR Query = From StudentIn  Students Group student by student. Major;  Foreach ( VaR S In Query) //  Enumerate the groups.  {Console. writeline (  "  {0}  "  , S. Key );  Foreach ( VaR T In S) //  Enumerate The items in the group. Console. writeline ( "  {0}, {1}  "  , T. lname, T. fname );}} 

 

 

 

21.5.9 query continuation

The query continuation clause can accept a portion of the query results and assign a name to them, which can be used in another part of the query. The query continuation syntax is as follows.

Example:

The query connects GroupA and groupB and is named groupaandb. Then, a simple SELECT statement is executed from GroupA and groupB.

 Static   Void  Main (){  VaR GroupA = New [] { 3 , 4 , 5 , 6  };  VaR GroupB = New [] { 4 , 5 , 6 , 7  };  VaR Someints = From A In  GroupA join B  In  GroupB On A equals B into groupaandb  //  Query continues                             From C In Groupaandb  Select  C;  Foreach ( VaR A In  Someints) console. Write (  "  {0}  "  , );} 

 

 

 

21.6 standard query Operators

The standard query operator is composed of a series of API methods. It allows us to query any. Net array and set.

1. The queried set object is calledSequenceIt must implement the ienumerable <t> interface, and T is the type.

2. standard query operators use method syntax.

3. Some operators return ienumerable objects (or other sequences), while others return scalar. The operator that returns the scalar is executed immediately, and the return value that replaces the enumerated type object is delayed iteration.

 Class  Program {  Static   Int [] Numbers = New   Int [] {2 , 4 , 6  };  Static   Void  Main (){  Int Total = Numbers. sum ();  Int Howtasks = Numbers. Count ();  //  ↑  // Scalar Object Sequence Operators Console. writeline ( "  Total: {0}, Count: {1}  "  , Total, howtasks );}} 

 

 

 

21.6.3 delegate as parameter

The query expression count is defined as follows:

The first parameter of each operator is the reference of the ienumerable <t> object, followed by any type of parameter. Many operators accept generic delegation as parameters. Generic delegation is used to provide users with customCode.

To explain this, we first start by demonstrating Several usage methods of the count operator. The Count operator is overloaded and has two forms. First,

Public Static IntCount <tsource> (ThisIenumerable <tsource> source );

Similar to all extension methods, we can use standard static methods or instance methods on an extension class, as shown below:

 
VaRCount1 = system. LINQ. enumerable. Count (intarry );//Static Method form
 
VaRCount2 = intarry. Count ();//Instance method form

Query the total number of int in the given integer array. However, if we want to see the total number of odd number elements in the array, we must specify the second form of the count method for implementation, as shown below.It has a generic delegate as its second parameter.. When calling, we must provide a delegate object that accepts input parameters of a single tsource type and returns a Boolean value. The Return Value of the delegate code must specify whether the element is included in the total number.

 
Public Static IntCount <tsource> (ThisIenumerable <tsource> source, func <tsource,Bool> Predicate );

For example, if a Lambda expression returns true if the input value is an odd number, otherwise false is returned.If it is an odd number, count will include this element in the total number.

 Static   Void  Main (){ Int [] Intarray = New   Int [] { 3 , 4 , 5 , 6 , 7 , 9  };  VaR Countodd = intarray. Count (n => N % 2 = 1  ); Console. writeline ( "  Count of odd numbers: {0}  "  , Countodd );} 

3,5, 7,9 is an odd number

Count of odd numbers: 4
Press any key to continue...

 

 

21.6.4 pre-defined delegation type of LINQ

LINQ defines two sets of generic delegation types used with standard query operators. They are func and action delegates, each with 17 members.

(Translate FUNC: Functions and functions)

1. tr carries the returned values and is always at the end of the Type parameter list.

2. Note that the return type parameter hasOutSo that it can be changed, that is, the declared type or the type derived from this type can be accepted.

3. The input parameter hasInThat is, you can accept the declared type or the type derived from this type.

Let's take a look at the count statement. It is found that the second parameter must be a delegate object, which accepts a single T type parameter as a method parameter and returns a bool type value.

 
Public Static IntCount <tsource> (ThisIenumerable <tsource>Source, func<Tsource,Bool>Predicate );//Zookeeper//Parameter type return type

The parameter delegate that generates a Boolean value is called a predicate.

The following are four action delegates. But there is no return value, similar to the func delegate. All parameter types are invert.

 

 

 

21.6.5 example of using delegate Parameters

For example, in the following code, we first declare the isodd method, which accepts a single int type parameter and returns the bool value indicating whether the input parameter is an odd number. The main method does the following.

1. Declares the int array as the data source.

2. It creates a delegate object whose type is func <int, bool> named MYDEL and uses the isodd method to initialize the delegate object. Note that we do not need to declare the func delegate type, because LINQ is predefined.

3. It uses the delegate object to call count.

 Static   Bool Isodd ( Int X)//  Method to be used by the delegate object  {  Return X % 2 = 1 ; //  Return true if X is odd.  }  Static   Void  Main (){  Int [] Intarray = New  Int [] { 3 , 4 , 5 , 6 , 7 , 9  }; Func < Int , Bool > MYDEL = New Func < Int , Bool > (Isodd ); // Delegate object              VaR Countodd = intarray. Count (MYDEL ); //  Use delegate  Console. writeline (  "  Count of odd numbers: {0}  "  , Countodd );} 

 

 

 

21.6.6 example of using Lambda expression Parameters

If a method is called elsewhere, not just to initialize the delegate object, the previous method is good. However, if you want a more concise and localized method, use the lambda operator.

 Static  Void  Main (){  Int [] Intarray = New   Int [] { 3 , 4 , 5 , 6 , 7 , 9  };  VaR Countodd = intarray. Count (x => X % 2 =1  ); Console. writeline (  "  Count of odd numbers: {0}  "  , Countodd );} 

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.