Projection operation of LINQ query operations

Source: Internet
Author: User

Projection operation, at first glance do not know what to say. So what is a projection operation? In fact, is the select operation, the name of the strange. Is the same as the select operation in a LINQ query expression. It is able to select elements in the data source and specify the representation of the elements. The projection operation consists of the following 2 actions:

1. The select operation projects the elements in the data source into a new sequence and specifies the type and representation of the element.

2, SelectMany operation, you can also project the elements in the data source into a new sequence, and specify the type and representation of the element. However, the operation can apply the function to more than one sequence and combine the results into a single sequence.

Let's take a look at each of these two operations.

Select operation

A select operation that projects elements from the data source into a new sequence and specifies the type and representation of the element. is similar to the select operation in a LINQ query expression. The Select operation applies a function to a sequence and produces a new sequence. The enumerable Select () prototype is as follows:

 Public Static Ienumerable<tresult> Select<tsource, tresult> ( This ienumerable<tsource> source, Func< TSource, tresult> selector);

Where source represents the data source, selector represents the transformation function for the mapping element. TSOURCE Specifies the parameter type, TResult specifies the return value type of the selector function.

Below we create an integer array ints, containing 10 elements. Use Select to calculate the remainder of each element divided by 5.

1  Private voidslectquery ()2         {3             int[] INTs =New int[Ten];4 5              for(inti =0; I <Ten; i++) Ints[i] =i;6 7             //Querying Data8             varresult = INTs. Select (x =%5);9 Ten             foreach(varIteminchresult) One             { AResponse.Write (item+"</br>"); -             } -}

Let's look at the output:

SelectMany operation

SelectMany and select are similar, the main difference is that you can apply a function to multiple sequences and merge the results into a new sequence. The prototype of the enumerable SelectMany () operation is as follows:

     Public StaticIenumerable<tresult> Selectmany<tsource, Tresult> ( ThisIenumerable<tsource> source, Func<tsource, ienumerable<tresult>>selector);  Public StaticIenumerable<tresult> Selectmany<tsource, Tresult> ( ThisIenumerable<tsource> Source, Func<tsource,int, ienumerable<tresult>>selector);  Public StaticIenumerable<tresult> Selectmany<tsource, TCollection, tresult> ( ThisIenumerable<tsource> Source, Func<tsource,int, Ienumerable<tcollection>> collectionselector, Func<tsource, TCollection, TResult>resultselector);  Public StaticIenumerable<tresult> Selectmany<tsource, TCollection, tresult> ( ThisIenumerable<tsource> source, Func<tsource, ienumerable<tcollection>> CollectionSelector, Func< TSource, TCollection, tresult> resultselector);

Source represents the data source, and selector represents the transformation function for the mapping element. Collectionselector represents a collection of transformation functions for a mapped element. Let's take a look at a concrete example.
1. Create two integer array inta,intb. Their values are {1,2,3},{4,5,6}, respectively.

2. Create a sequence of list<int[]> and add the INTA and INTB arrays to the sequence.

3. Operation List<int[]> Sequence with SelectMany

Let's start with the select operation:

1 Private voidslectmanyquery ()2         {3             int[] Intsa =New int[] {1,2,3};4             int[] INTSB =New int[] {4,5,6};5 6ilist<int[]> LST =Newlist<int[]>();7 8 lst. ADD (Intsa);9 Ten lst. ADD (INTSB); One  A             //Querying Data -             varresult = LST. Select (x=>x); -  the  -             foreach(varIteminchresult) -             { -Response.Write (item+"</br>"); +             } -}

Look at the results:

We now operate with SelectMany:

1 Private voidslectmanyquery ()2         {3             int[] Intsa =New int[] {1,2,3};4             int[] INTSB =New int[] {4,5,6};5 6ilist<int[]> LST =Newlist<int[]>();7 8 lst. ADD (Intsa);9 Ten lst. ADD (INTSB); One  A             //Querying Data -             varresult = LST. SelectMany (x=>x); -  the  -             foreach(varIteminchresult) -             { -Response.Write (item+"</br>"); +             } -}

Look at the results of the operation:

We see that the select Operation only operates on the list object, and SelectMany is the final merge of each operation in list<int[]> into a new result set.

We can open the comparison through the source code:

The iterator that the select operation eventually calls:

We can see that it only makes a foreach iteration of the data source.

SelectMany Operation:

SelectMany operation, you can see that the foreach iteration is performed first, and then a foreach iteration is made to each item of the dataset in the iteration. So SelectMany is able to combine the results after multiple sequences are manipulated.

Projection operation for LINQ query Operations

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.