Select and selectmany operations in LINQ

Source: Internet
Author: User

Select ()AndSelectmany ()Is to generate one or more result values based on the source value.Select ()Generate a result value for each source value. Therefore, the overall result is a set with the same number of elements as the source set. In contrast,Selectmany ()A single overall result is generated, which contains a collection of concatenation Subsets from each source value. Passed as a parameterSelectmany ()The conversion function must return a sequence of enumerated values for each source value. Then,Selectmany ()These enumerated sequences are concatenated to create a large sequence.

The following two illustrations demonstrate the conceptual differences between the operations of the two methods. In each case, assume that the selector (conversion) function selects an array composed of flower data from each source value.

DescriptionSelect ()Returns a set with the same number of elements as the source set.

DescriptionSelectmany ()How to concatenate the intermediate array sequence into a final result value, which contains each value in each intermediate array.

Sample Code

Comparison of the following examplesSelect ()AndSelectmany (). The Code extracts the first two items from each flower name list in the source set to create a "bouquet ". In this example, the conversion function {
Track ('ctl00 _ mtcs_main_ctl39_ctl00_contenthere | ctl00_mtcs_main_ctl39_ctl00_ctl00 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/bb548891.aspx "> select <(of <(tsource, tresult>) (ienumerable <(of <(tsource> ), func <(of <(tsource, tresult>) the "single value" used is a set of values. This requires additionalForeach(In Visual BasicFor each) To enumerate each string in each sub-sequence.

 

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Text. regularexpressions;
Using system. IO;
Using system. xml;

Namespace csharptest
{
Class bouquet
{
Public list <string> flowers {Get; set ;}
}

Class Program
{
Static void main (string [] ARGs)
{
Console. writeline ("--------- result ------------");
Ienumerable <int []> II = compute (1, 3, 5, 3 );
Foreach (INT [] I1 in II)
{
Foreach (INT I2 in I1)
Console. Write (I2 );
Console. writeline ();
}

Console. writeline ("-------- string. Concat connects to the string representation of elements in the specified object array --------");
Console. writeline (string. Concat (New int [] {1000, 22 }));
Console. writeline ("----------- enumerable. Range generate the sequence of integers in the specified range ----------");
VaR list = enumerable. Range (1, 4 );
Foreach (VAR item in List)
{
Console. writeline (item );
}
Console. writeline ("------- convert a number to ienumerable <int []> type --------------");
II = from item
In list
Where item = 3
Select New int [] {item };

Foreach (INT [] I1 in II)
{
Foreach (INT I2 in I1)
Console. Write (I2 );
Console. writeline ();
}

// The following is an example of select selectmany on msdn.
List <bouquet> bouquets = new list <bouquet> ()
{
New bouquet {flowers = new list <string> {"sunflower", "Daisy", "daffodil", "Larkspur "}},
New bouquet {flowers = new list <string> {"Tulip", "Rose", "orchid "}},
New bouquet {flowers = new list <string> {"gladiolis", "Lily", "Snapdragon", "Aster", "protea "}},
New bouquet {flowers = new list <string> {"Larkspur", "lilac", "Iris", "Dahlia "}}
};

// ************ Select ***********
Ienumerable <list <string> query1 = bouquets. Select (Bq => Bq. Flowers );

// ********** Selectparameters *********
Ienumerable <string> query2 = bouquets. selectmany (Bq => Bq. Flowers );

Console. writeline ("results by using select ():");
// Note the extra foreach loop here.
Foreach (ienumerable <string> collection in query1)
Foreach (string item in Collection)
Console. writeline (item );

Console. writeline ("/nresults by using selectpipeline ():");
Foreach (string item in query2)
Console. writeline (item );

 

}

/// <Summary>
/// Four integers, sum, Min, Max, and N, are given. Please output the sum of all integers that divide sum into N. Each positive integer k meets the following conditions: min <= k <= max. The N integers can be repeated. However, due to the addition exchange rate, the values of 1 + 2 and 2 + 1 are repeated.
/// For example, sum = 5, n = 3, min = 1, max = 3. At this time, there are only two possible splitting methods:
/// 1 + 1 + 3
/// 1 + 2 + 2
/// </Summary>
/// <Param name = "min"> </param>
/// <Param name = "Max"> </param>
/// <Param name = "sum"> </param>
/// <Param name = "count"> n </param>
/// <Returns> </returns>
Public static ienumerable <int []> compute (INT min, int Max, int sum, int count)
{
VaR list = enumerable. Range (Min, max-min + 1 );

If (COUNT = 1)
Return
From item
In list
Where item = sum
Select New int [] {item };

Return list. selectparameters
(
Number => compute (number, Max, Sum-number, Count-1). Select
(
Item => item. Concat (New int [] {number}). toarray ()
)
);
}

}
}

See msdn: http://msdn.microsoft.com/zh-cn/library/bb546168.aspx#Mtps_DropDownFilterText

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.