SelectMany in LINQ (Select many Operator in LINQ)

Source: Internet
Author: User

The SelectMany function can expand the returned list to list. The best way to understand this sentence is to pass an example:

 Public classperson{ Public stringFirstName;  Public stringLastName;  Public stringGender;  Public intanualsalary;  Publiclist<string>PhoneNumber;  Public StaticList<person>Getallperson () {List<Person> personinlist =NewList<person>()         {            NewPerson () {FirstName="Jonh", LastName="Liu", Gender="Male", Anualsalary=70000, PhoneNumber=Newlist<string>()                 {                     "0101235566",                     "12056878445"                 }            },            NewPerson () {FirstName="Sara", LastName="Guo", Gender="Famale", Anualsalary=100000, PhoneNumber=Newlist<string>()                 {                     "13356985469",                     "15536215459"                 }            },            NewPerson () {FirstName="Marttin", LastName="Zeng", Gender="Male", Anualsalary=80000, PhoneNumber=Newlist<string>()                 {                     "15862354569",                     "13756542698",                     "15936545269"                 }            },            NewPerson () {FirstName="Sccot", LastName="Jiang", Gender="Male", Anualsalary=90000, PhoneNumber=Newlist<string>()                 {                     "02388695645"                 }            }        }; returnpersoninlist; }}

In the code above, the person class has a property named PhoneNumber with the list<string> type. If we're going to get all the Phonenumer for the list<person> of sex as "male" we can use the SELECT statement:

List<person> personinlist =Person.getallperson (); IEnumerable<List<string>> marttinsphonenumberlist = personinlist.where (p = P.gender = ="Male"). Select (p =p.phonenumber); foreach(list<string> eachphonenumberlistinchmarttinsphonenumberlist) {     foreach(stringEachphonenumberincheachphonenumberlist)     {Console.WriteLine (eachphonenumber); } Console.WriteLine (New string('-', -)); }

As you can see, the result we filter with the Select function is the ienumerable<list<string>> type. This is well understood, because PhoneNumber is inherently a list<string> type in the person class. But what happens if we use the SelectMany function? Take a look at the following code:

list<person> personinlist = Person.getallperson () IEnumerable<string"Male "). SelectMany (p = p.phonenumber); foreach (string in allphonenumberinlist) {    Console.WriteLine (eachphonenumber);}

In fact, we must use ienumerable<string> to receive the return value of SelectMany! Because SelectMany will further expand the returned type of list<string> PhoneNumber to the ienumerable<string> type. This also explains the meaning of the "SelectMany function can expand the returned list to list" At the beginning of the article. Give another example if we run the following code:

list<person> personinlist = Person.getallperson () IEnumerable<char"Sccot "). SelectMany (p = p.lastname); foreach (char in allcharoflastname) {    Console.WriteLine (Eachchar);}

The above code first selects the person with the FirstName as "Sccot" and returns its LastName with SelectMany. Note that the result of the LastName type is String,selectmany is to expand the string to a list of char types.

The SelectMany also has additional overloads. For example, if we want to display fullname and corresponding phonenumber at the same time, we can use the following code to achieve the goal:

List<person> personinlist =Person.getallperson ();varresult = Personinlist.where (p = = P.gender = ="Male"). SelectMany (P = p.phonenumber, (person, phonenumber) =New{FullName = person. FirstName +" "+ person. LastName, PhoneNumber =PhoneNumber});foreach(varIteminchresult) {Console.WriteLine (string. Format ("Fullname:{0}\r\nphonenumber:{1}", item. FullName, item. PhoneNumber));}

Reference: Http://stackoverflow.com/questions/958949/difference-between-select-and-selectmany

SelectMany in LINQ (Select many Operator in LINQ)

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.