A preliminary study on new features of C # 3.0 PART6: Using query expressions

Source: Internet
Author: User
Query expression (Expression)


Everyone should be familiar with the SQL statement, before C # 2.0, embedded in the code in the SQL is the following:


1public void Test ()


2{


3SqlConnection C = new SqlConnection (...);


4 C.open ();


5 SqlCommand cmd = new SqlCommand (


6 @ "Select C.name, C.phone//queries in quotes


7 from Customers C


8 WHERE c.city = @p0 "


9);


-cmd.     parameters["@po"] = "London"; Arguments loosely bound


DataReader dr = C.execute (cmd);


(DR). Read ()) {


String name = r.getstring (0);


String phone = r.getstring (1); Results loosely typed


DateTime date = R.getdatetime (2); Compiler can ' t help catch mistakes


16}


R.close ();


18}


in C # 3.0, we can easily use the "SQL statement" to other places, of course, this is not a real SQL statement ~ ~


I think I'll use a lot of the following similar code in the future development process:


1class Program


2 {


3 static void Main (string[] args)


4 {


5 var contacts = new list<contact> ();


6


7 Contacts. ADD (New Contact ("Michael", "520-331-2718",


8 "33140 SW Liverpool Lane", "WA");


9 Contacts. ADD (New Contact ("Jennifer", "503-998-1177",


"1245 NW baypony Dr", "OR"));


one by one contacts. ADD (New Contact ("Sean", "515-127-3340",


"55217 SW estate Dr", "WA");


13


var wacontacts =


from C in Contacts


where c.state = = "WA"


Select New {c.name, c.phone};


18


Console.WriteLine ("Contacts in the state of Washington:");


foreach (var c in wacontacts)


21 {


Console.WriteLine ("Name: {0}, Phone: {1}", C.name, C.phone);


23}


24}


25}


26


class Contact


28 {


public string Name;


public string Phone;


public string address;


public string State;


33


(string name, string phone, string address, string state)


35 {


this. name = name;


. Phone = phone;


this. address = address;


. state = State;


40}


41}


where the code appears:


1var wacontacts =


2 from C in Contacts


3 where C.state = = "WA"


4 Select New {c.name, c.phone};

Does
have a great similarity to our familiar SQL statements? of course!


in the end is the SQL dream of C #, or C # Dream of SQL ...





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.