C # Learning Log Day7 A preliminary attempt to--------------LINQ and LAMDA statements and the generation of XML

Source: Internet
Author: User

LINQ is a kind of information query statement integrated in computer language, which is the most noticeable function in c#3.0.

In C #, LINQ statements are written in two ways.

The first is similar to an SQL statement:

ienumerable<customer> result = from in      customers      where  "Select Customer;     

The second way is closer to the C # statement:

ienumerable<customer> result =      "Donna")    . Select (customer = customer);   

This kind of writing is easy to understand, so I think this is a better notation.

The LAMDA statement, which is populated with the where and select, is a simplification of the delegate, which facilitates the readability of the code.

The form of the LAMDA expression is usually the case

People=>people.age>30

The first people refers to the parameters passed in, = = is the specific symbol of the LAMDA expression, and to the right is an expression, in the query statement, the return value of the expression following this symbol is usually of type Boolean. For example, the above statement can be used in where to filter out people older than 30.

The following is a simple use of LINQ and lambda expressions

Customer class:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication3{ Public classCustomer { Public stringFirstName {Get;Set; }  Public stringLastName {Get;Set; }  Public stringhomeaddress {Get;Set; } //Now override the ToString function of Object class.         Public Override stringToString () {return string. Format ("{0} {1}\n enmail:{2}", FirstName, LastName, homeaddress); }         Public StaticList<customer>createcustomerlist () {List<Customer> customers =NewList<customer>            {                 NewCustomer {FirstName ="Orlando", LastName ="Gee", homeaddress ="[email protected]"},                 NewCustomer {FirstName ="Keith", LastName ="Harris", homeaddress ="[email protected]" },                 NewCustomer {FirstName ="Donna", LastName ="Carreras", homeaddress ="[email protected]" },                 NewCustomer {FirstName ="Janet", LastName ="Gates", homeaddress ="[email protected]" },                 NewCustomer {FirstName ="Lucy", LastName ="Harrington", homeaddress ="[email protected]" }            }; returncustomers; }    }}

Query the main function for records beginning with D

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication3{classProgram { Public Static voidMain () {List<Customer> customers =customer.createcustomerlist (); IEnumerable<Customer> result =customers. Where (Customer= Customer. Firstname.startswith ("D")); foreach(Customer Customerinchresult) {Console.WriteLine (Customer.            ToString ()); }        }    }}

About XML I used a database to simply create an XML document

  Public Static voidMain () {List<Customer> customers =customer.createcustomerlist (); XmlDocument Customerxml=NewXmlDocument (); XmlElement Rootelem= Customerxml.createelement ("Customers");            Customerxml.appendchild (Rootelem); foreach(Customer Custinchcustomers) {XmlElement Customerelm= Customerxml.createelement ("Customer"); XmlElement Firstelm= Customerxml.createelement ("FirstName"); Firstelm.innertext=Cust.                FirstName;                Customerxml.appendchild (Firstelm); XmlElement Second= Customerxml.createelement ("LastName"); Second. InnerText=Cust.                LastName;                Customerxml.appendchild (second); XmlElement Third= Customerxml.createelement ("EmailAddress"); Third. InnerText=Cust.                Address;                Customerxml.appendchild (third);            Rootelem.appendchild (Customerelm);        } Console.WriteLine (Customerxml.outerxml); }

The result of the operation is this

 <customers><customer><firstname>orlando</firstname><lastname>gee</ Lastname><emailaddress>[email protected]</emailaddress></customer><customer> <firstname>keith</firstname><lastname>harris</lastname><emailaddress>[email  protected]</emailAddress></customer><customer><firstName>Donna</firstName> <lastname>carreras</lastname><emailaddress>[email protected]</emailaddress></ customer><customer><firstname>janet</firstname><lastname>gates</lastname>< Emailaddress>[email protected]</emailaddress></customer><customer><firstname> Lucy</firstname><lastname>harrington</lastname><emailaddress>[email protected] </emailaddress></customer></customers> 

Here XmlElement Firstelm = customerxml.createelement ("firstName"); statement is defined Firstelm tag, which is not in HTML

The generation of XML requires the use of System.Xml.linq; namespaces.

C # Learning Log Day7 A preliminary attempt to--------------LINQ and LAMDA statements and the generation of XML

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.