LINQ to object--immediate execution of enumerable class methods

Source: Internet
Author: User

In the previous approach to the enumerable class that LINQ to object--deferred, the next step is to say the enumerable class method that LINQ to object--immediately executes.

1.ToArray sequence Conversion array

list<string> names_list =Newlist<string> {"Zhang San","Fan Bing","Li bingbing","Michael Jackson","John Doe","Harry","Zhao Liu","Tianqi" }; string[] Takenames_arry =names_list.            ToArray (); string[] Takenames_arry2 = ( fromNameinchnames_listSelectName). Take (4).            ToArray (); foreach(varNameinchTakenames_arry2)            {Console.WriteLine (name); } Console.WriteLine ("----------------------------"); Console.readkey ();

Operation Result:

2.ToList sequence converted to list<t>

            string[] Names_array = {"Zhang San","Fan Bing","Li bingbing","Michael Jackson","John Doe","Harry","Zhao Liu","Tianqi" }; List<string> takenames_list =Names_array.            ToList (); List<string> Takenames_list2 = ( fromNameinchNames_arraySelectName). Skip (4).            ToList (); foreach(varNameinchtakenames_list2)            {Console.WriteLine (name); } Console.WriteLine ("----------------------------"); Console.readkey ();

Operation Result:

3.todictionary Converting a sequence to a generic dictionary<tkey,tvalue>

4.ToLookup is used to convert a sequence to a generic lookup<tkey,tvalue>

5.sequenceequal Comparison of two sequences for equality

            bool sequence_equal = Names_array. SequenceEqual (names_list);             bool sequence_equal2 = Names_array. Skip (1). Take (2). SequenceEqual (names_list. Take (32));            Console.WriteLine ("{0},{1}", Sequence_equal, sequence_equal2);            Console.WriteLine ("----------------------------");            Console.readkey ();

Operation Result:

6. First returns a sequence that satisfies a conditional element

            var first_name = Names_array. First ();             var 3 );            Console.WriteLine ("{0},{1}", first_name, first_name2);            Console.WriteLine ("----------------------------");            Console.readkey ();

Operation Result:

7.FirstOrDefault return sequence first satisfies the condition element, if not found, returns the default value

            var first_or_default_name = Names_array. FirstOrDefault ();             var " 123 " );            Console.WriteLine ("{0},{1}", First_or_default_name, First_or_default_ NAME2);            Console.WriteLine ("----------------------------");            Console.readkey ();

Operation Result:

8. Last returns a sequence that satisfies the condition element

            var last_name = Names_array. Last ();             var 3 );            Console.WriteLine ("{0},{1}", last_name, last_name2);            Console.WriteLine ("----------------------------");            Console.readkey ();

Operation Result:

9.LastOrDefault Returns the last element of the sequence that satisfies the condition and returns the default value if not found

            var last_or_default_name = Names_array. LastOrDefault ();             var " 123 " );            Console.WriteLine ("{0},{1}", Last_or_default_name, Last_or_default_ NAME2);            Console.WriteLine ("----------------------------");            Console.readkey ();

Operation Result:

Ten. Single Returns the only element in the sequence, note: If the sequence contains multiple elements, a run error is raised

            Try            {                varSingle_name =Names_array.            Single (); }            Catch(Exception ex) {Console.WriteLine (ex).            Message); } Console.WriteLine ("----------------------------"); varSingle_name2 = Names_array. Single (n = n = ="Zhang San"); Console.WriteLine ("{0}", single_name2); Console.WriteLine ("----------------------------"); Console.readkey ();

Operation Result:

Singleordefault Find the element that satisfies a certain condition in the sequence, note that if the sequence is empty, the default value is returned, and if the sequence contains more than one element, a run error is raised

            Try            {                varSingle_or_default_name = enumerable.empty<int>().                Singleordefault (); Console.WriteLine ("{0}", single_or_default_name);//no error, return default value if sequence is empty            }            Catch(Exception ex) {Console.WriteLine (ex).            Message); } Console.WriteLine ("----------------------------"); Try            {                varSingle_or_default_name2 =Names_array.                Singleordefault (); Console.WriteLine ("{0}", single_or_default_name2);//error, sequence contains multiple line errors            }            Catch(Exception ex) {Console.WriteLine (ex).            Message); } Console.WriteLine ("----------------------------"); varSingle_or_default_name3 = enumerable.empty<string> (). DefaultIfEmpty ("Default Value").            Singleordefault (); Console.WriteLine ("{0}", Single_or_default_name3); Console.WriteLine ("----------------------------"); varSingle_or_default_name4 = Names_array. Singleordefault (n = n = ="123"); Console.WriteLine ("{0}", Single_or_default_name4); Console.WriteLine ("----------------------------"); Console.readkey ();

Operation Result:

ElementAt . Gets the element at the specified index

            var element_at_name = Names_array. ElementAt (3);            Console.WriteLine ("{0}", element_at_name);            Console.WriteLine ("----------------------------");            Console.readkey ();

Operation Result:

Elementatordefault Gets the element at the specified index, and returns the default value of the element type if the index is exceeded

            var element_at_or_default_name = Names_array. Elementatordefault (5);             var element_at_or_default_name2 = Names_array. Elementatordefault (ten);            Console.WriteLine ("{0},{1}", Element_at_or_default_name, Element_at_or_ DEFAULT_NAME2);            Console.WriteLine ("----------------------------");            Console.readkey ();

Operation Result:

All elements in the all sequence meet the criteria

Whether an element in the any sequence exists or satisfies a condition

Contains Determines whether an element is in a sequence

Thecount sequence contains the number of elements

LongCount . Gets the number of elements of a Int64 type

Aggregate to accumulate sequence elements

Sums ofSum sequences

Average sequence Average

Minimum value ofmin sequence

Max value of themax sequence

To be Continued ...

LINQ to object--immediate execution of enumerable class methods

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.