linq-Basic Query Operator Select/where statement

Source: Internet
Author: User

The basic query operations in LINQ are the same as those in SQL, and for similarities and differences, let's look at the following:

1) Select

Grammar:

<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" > public static ienumerable<tresult> select<tsource,tresult> (this ienumerable<tsource>source, Func<tsource,tresult>selector) </span>
Description:
    • The Select method itself is a generic collection extension method
    • It acts on the ienumerable<tsource> type
    • It only accepts one func<tsource,tresult> type parameter
    • Func<tsource,tresult> is a generic delegate, located under the System namespace, in the system Core DLL, where selector is an extractor.

2) Where

Grammar:

<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >public static ienumerable<tsource> where<tsource> (This ienumerable<tsource>source,func< Tsource,bool>predicate) </span>

Description:
    • The Where method is also a generic extension method
    • It acts on the ienumerable<tsource> type as with select ()
    • It accepts only one func<tsource,bool> generic delegate parameter
    • The predicate here is a condition of judgment.

3) Examples:

First, let's write an extension class that provides the output method for ienumerablet<string>

<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >//Extension classes, as long as the static can be public    static class Extraclass    {        //for ienumerablet<string> to provide output method        public static void Print (this ienumerable<string> ie)        {            //Gets the interface that can be traversed            ienumerator<string> result = ie. GetEnumerator ();            Console.WriteLine ("\ n-------------------------------------\ n");            while (result. MoveNext ())            {                Console.WriteLine (result). current);            }            Console.WriteLine ("\ n---------------------------------------\ n");}    } </span>
write a generic collection and store several data for it:
<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >  private void Btnselect_click (object sender, EventArgs e)        {            //linq to Objects            //generic collection data persons            list<string> persons = new list<string> ();            Persons. Add ("Li si");            Persons. Add ("Meng Meng");            Persons. ADD ("Huo Huo");            Persons. Add ("Niu nan");            Persons. Add ("Hu Hu");            Persons. Add ("Tu zi");            Persons. ADD ("Huo er");            Persons. Add ("Huo Xu");</span>
This allows us to display the output according to the selection:

1. Output all elements in person:

<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >//Output all the elements in person var result=persons. Select (p = p);</span>

Show Results:


2, output person in the surname Huo (the following three statements all show the same effect)

<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >//Output person in the surname Huo (the following three statements must answer the same effect)    //var result=persons. Where (P=>p.startswith ("Huo"));   var result = persons. Select (p=>p). Where (p = p.startswith ("Huo"));  var result = persons. Where (P = = P.startswith ("Huo")). Select (p=>p);</span>
Show the effect:

Of course, you can also directly use where, using bool delegate parameters to execute,

<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >  var result=persons. Where (P=>judge (P));                   Result. Print ();        }        public bool Judge (string s)        {            if (S.startswith ("Huo"))            {                return true;            }            else            {                return false;            }        } </span>
Displays the same effect, but directly shows the use of the WHERE () statement

Examples are just a few common query statements, others in the continuing study!


linq-Basic Query Operator Select/where statement

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.