The use of the Select and where of Linq

Source: Internet
Author: User

Recently began to learn LINQ. You can also summarize it for your convenience.

Select is similar to select in SQL, which outputs what we want, and feels more powerful in LINQ.

Linq can operate on collections such as arrays, generics, and so on, where we give examples of generic types. Build a class like this:

 Public classCustomer { PublicCustomer (stringFirstName,stringLastName,stringCity ) {FirstName=FirstName; LastName=LastName; City=City ; }   PublicCustomer () {} Public stringFirstName {Get;Set; }  Public stringLastName {Get;Set; }  Public stringCity {Get;Set; } }  

1 . Select Out object,Cust is a Customer object.

Static voidMain (string[] args) {List<Customer> customers =NewList<customer>()               {                   NewCustomer ("Jack","Chen","London"),                  NewCustomer ("Sunny","Peng","Shenzhen"),                  NewCustomer ("Tom","Cat","London")              }; //using ienumerable<t> as a variable            varresult = fromCustinchCustomerswhereCust. City = ="London"                          SelectCust; foreach(varIteminchresult) {Console.WriteLine (item. FirstName+":"+item.              City);          } console.read (); }  

2. Select the attribute field for the object

var result =from in"London"           Select Cust. City  

Multiple properties are to be used with new {} as follows:

var result =from inwhere"London"                          Select  new{Cust. City,cust. FirstName};  

3. Rename, city and name are random

varresult = fromCustinchCustomerswhereCust. City = ="London"                          Select New{City=cust. City, name ="name"+ Cust. firstname+""+Cust.              LastName}; foreach(varIteminchresult) {Console.WriteLine (Item.name+":"+item.city); }  

4. Direct instantiation of objects

var result =from inwhere"London"           SelectNew

5. nested Select in Selec

var result = from in Customers  where"London"              Selectnew{city = Cust. City,   name=from inwhere"London" Select Cust1. LastName  };  

The use of the Select and where of 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.