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