Enumerators and LINQ

Source: Internet
Author: User

Iterator: Enumerator If you are creating a class that behaves and behaves like a collection, it is convenient to allow users of the class to enumerate the members of the collection using the foreach statement.  We'll start by creating a simplistic list box that will contain an array of 8 strings and an integer that records how many strings have been added to the array. The constructor initializes the array and populates it with the parameters passed in. 


UsingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacenettest{/// <summary> ///Iterator: Enumerator///Test Enumerator, inherit IEnumerable, implement IEnumerator<T>GetEnumerator ()/// </summary> /// <typeparam name= "T" ></typeparam> Public classListbox<t>: ienumerable<t> { PublicList<t> Strings =NewList<t>(); PublicListBox (paramst[] initialstrings) { foreach(T sinchinitialstrings) {Strings.add (s); }} IEnumerator<T> Ienumerable<t>. GetEnumerator ()//both this method and the following methods must be implemented { foreach(T sinchStrings) { yield returns; } }


The GetEnumerator method uses a new yield statement. The yield statement returns an expression. The yield statement appears only in the iteration block,
and returns the value expected by the foreach statement. That is, every call to GetEnumerator//will produce the next string in the collection; All state management has been done for you!

        IEnumerator ienumerable.getenumerator ()// This method and the method above must be implemented         {             Throw New notimplementedexception ();     }}}
classProgram {Static voidMain (string[] args) {            //programtest p = new Programtest (3); //p.teststring ();ListBox<string> lb =Newlistbox<string> ("WEA","b","C","D","e","F","g","h"); foreach(stringSinchlb) {Console.Write (S+" ");            } Console.WriteLine (); ListBox<int> LB1 =Newlistbox<int> (4,5,6, the); foreach(intSinchLB1) {Console.Write (S+" "); }
ListBox<string> lb =Newlistbox<string> ("WEA","b","C","D","e","F","g","h"); foreach(stringSinchlb. Strings)//Call Property {Console.Write (S+" "); }

            Console.ReadLine ();        }    }
the basic format of LINQ is as follows: var  from inch where  by-< expressions >
Group GROUP clause statement format:varstr = fromPinchThe personlist group p by p.agegroup clause groups the data in the data source, while traversing the data element does not traverse the element as directly as the previous section, because the group clause returns the element type IGrouping<TKey,TElement>sequence of objects that must be nested within a loop to query the corresponding data element. When using the group clause, there is no SELECT clause at the end of the LINQ query clause, because the group clause returns an object sequence that can be used to find the elements of the corresponding object in the object sequence by iterating, and if the group clause is a grouping operation, You can not use the SELECT clause. ORDER BY SORT clause statement format:varstr = fromPinchPersonlist byP.ageSelectThe example code for descending order using the descending keyword in the P;orderby clause is as follows:varstr = fromPinchPersonlist byP.age DescendingSelectp; The order-by clause is also capable of ordering multiple conditions, and the sample code is simply separated by the "," Number:varstr = fromPinchPersonlist byP.age Descending,p.nameSelectThe p;join join clause can also use the JOIN clause to query a relational data source or data object in LINQ, but first the two data sources must have a certain connectionvarstr = fromPinchPersonlist Join carinchCarlist on P.cid equals car.cidSelectP

var innerjoinquery =      customers    in  distributors on Cust. City equals Dist. City    Selectnew {CustomerName = cust. Name, Distributorname = Dist. Name};

// Custquery is an ienumerable<igrouping<string, customer>> var custquery = from the      customers    Group Cust by Cust. City into CustGroup    where2by      custgroup.key     Select CustGroup;


Stringarrays ="Hua Ying Jie."; vars = fromArrainchArrays byArra DescendingSelectArra; List<Char> s2 = ( fromArrainchArrays byArra DescendingSelectarra). ToList (); Char[] S6 = ( fromArrainchArrays byArra DescendingSelectarra). ToArray (); IEnumerable<Char> s4 = fromArrainchArrays byArra DescendingSelectArra; IEnumerable<Char> s5 = arrays. OrderByDescending (n = n);//These five implementations have the same effect

Enumerators and 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.