C#LINQ technology SelectMany (...) The internal implementation of the conjecture

Source: Internet
Author: User

For the declaration: public static ienumerable<tresult> Selectmany<tsource, tresult> (This ienumerable<tsource> Source, Func<tsource, ienumerable<tresult>> selector), the form to invoke it is: Alist.selectmany (itm=>  Itm.listprop); Among the attributes in alist is the attribute Listprop of the collection . the Listprop collection element type is TResult.

For Alist.select (Itm=>itm.listprop), the return is ienumerable<list<tresult>>

Now for the declaration: public static ienumerable<tresult> Selectmany<tsource, TCollection, tresult>(this ienumerable<tsource> Source, Func<tsource, ienumerable<tcollection>> collectionSelector , Func<tsource, TCollection, TResult> Resultselector);

The fields in TResult can be made up of TSource and tcollection, or they can be composed only of certain fields of tcollection.

It can be assumed that there are Gradeid and ClassID fields and list<student> studs in class, and Student only classid without gradeid;

Ienumerable<tsource> is a collection of classes. And Ienumerable<tcollection> is a collection of student in class.

The form of calling SelectMany is classes. SelectMany (Class=>class.studs, (Class,stud) =>new{gradeid=class. Gradeid,studentid=stud. StudentID,...});

Where classes is source, Stud is the element in studs. And if there are two delegate arguments in the method, the second delegate typically needs to use the return value of the first delegate indirectly.

This example shows why a class is required in the second delegate.

1        //Called when it is source. SelectMany (I=>i.listprop, (i,s) => ...); I was the following ITM
TSource is similar to class; TCollection is similar toStudent; TResult is the type of the return value of the second delegate, which can be an anonymous type2 PublicIenumerable<tresult> selectmany<tsource,tcollection,tresult>(Source,collectionselector,resultselector)3 {4Ienumerable<tresult> Listresult =NewIenumerable<tresult>();
source is similar to the above classes 5 foreach(varItminchsource)6 {7 //collectionselector (ITM) returned is Itm.listprop;8 //the type of Itm.listprop is the tcollection type. 9Ienumerable<tcollection> blockcollection =collectionselector (ITM); Blockcollection similar to a class studs Ten //If there is no back resultselector then it will actually execute listresult.addrange (blocklist); One A //ienumerable<tresult> blockresult=new list<tresult> - foreach(varCitminchblockcollection)// similar to the above studs - { the //ITM is used here because the fields in TResult are not necessarily truncated in citm, but also include some amplification, amplification - //The field can be in ITM. For a chestnut: ITM is trade (with multiple order, which is blockcollection), while CITM is Order, - //The result returned at this time is not necessarily the new object that was obtained after the order's property was reduced, but also the blockcollection attribute in itm. - //other attributes, such as the name of the consignee and the attributes in order, are spliced. (Note that there is no consignee's name before order, because an order must be + //belongs to a specific trade, and a trade is generated by a buyer's purchase of items, so only the name of the recipient must be in the trade. Here we - //The attribute of the recipient's name is integrated into order to form ' order, which is TResult. + //So, it was not necessary to think of ITM before because I didn't understand the SelectMany function. Always think of a structure to be modified to form a new A //The structure is only to say that the original structure of the pruning, it is now known that the original structure can actually be increased, and with what increase, can be at //irrelevant data, or it can be the ancestor of the structure. - Listresult.add (Resultselection (ITM, CITM)); The return value of Resultselection (ITM,CITM) is the TResult object - } - } - returnListresult; -}

C#linq technology in SelectMany (...) The internal implementation of the inference

Related Article

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.