What is LINQ
- LINQ (pronounced as link) represents a language-integrated query (Language Integrated query)
- See more in LINQ
Anonymous type
The first member initializer is an assignment form, the second is a member access expression, and the third is the identifier form, as shown in the following example:
1 String sex = "male"; 2 var person = new {age = $, simpleclass.name, sex};3 Console.WriteLine ("{0},sex{1},age {2}", person. Name,person. Sex,person. Age);
Operation Result:
Note The identifier form and the access expression must be defined before the anonymous type declaration, sex is a local variable, and name is a static field of the class Simpleclass.
Although anonymous types are not visible in code, the Object Browser can see them. If the compiler encounters another anonymous type with the same parameter name, the same reference type name, and the same order, it reuses the type and creates a new instance directly without creating a new anonymous type.
query syntax and method syntax
- Query syntax is a declarative form and looks similar to SQL statements. Query syntax is written using query expressions.
- Method syntax is a form of command, and its use is a standard method call. A method is a set of methods called standard query operators.
- You can combine both of these forms in a query.
The sample code looks like this:
1 var query1 = from N in enumerable.range (1, ten) 2 where n < 8 3 select N; 4 Console.Write ("Query syntax gets results:"); 5 foreach (Var v in Query1) Console.Write ("{0}\t", V); 6 7 var Query2 = Enumerable.range (1, 8). Where (n = n < 8); 8 Console.WriteLine (""); 9 Console.Write ("method syntax gets result:"); (Var v in Query2) Console.Write ("{0}\t", v); 1112 int count = (from N in Enumerable . Range (1, ten), where n < 814 select n). Count (); Console.WriteLine (""); Console.WriteLine ("Combination of two methods: {0}", count);
Operation Result:
Structure of the query expression
1. FROM clause
- The FROM clause specifies the collection of data to use as the data source. It also introduces iterative variables, which sequentially represent each element of the data source.
1 int[] arr = {1, 5, 9, 8,, 7, 8, +,, +, 9,};2 var query = from N in arr//(n-> iteration variable) 3 where n < 10//using iteration variables 4 Select N; Use iteration variable 5 foreach (var v in query) Console.Write ("{0}\t", V), 6 Console.WriteLine ("");
Operation Result:
2. Join clause
- The join clause in LINQ is similar to join in SQL, and the join operation accepts two collections and then creates a temporary collection of objects, each containing all the fields from the original collection object, using joins to combine data from two or more collections.
The sample code is as follows:
1 public class Student 2 {3 public int Stid, 4 public string LastName, 5} 6 public class Coursestudent 7 {8 public String Coursename; 9 public int stid;10}1112 coursestudent[] studentincourses = new coursestudent[]{13 new coursestudent{coursename= "Art", S tid=1},14 new Coursestudent{coursename= "Art", stid=3},15 new Coursestudent{coursename= "History", stid=1},16 new Coursestudent{coursename= "History", stid=2},17 new Coursestudent{coursename= "Physics", stid=3}18};19 student[] Students = new student[] {new student{stid=1,lastname= "Zhang San"},21 new student{stid=2,lastname= "John Doe"},22 New student{stid= 3,lastname= "Harry"}23};2425//Get elective 2 classes above the name of the students in the var query = (from the s in the STUDENTS27 join C in studentincourses on S.stid equal S c.stid28 where (from X in studentincourses where X.stid==c.stid select X.stid). Count () >129 select S.lastname). Distinct (); (var v in query) Console.Write ("{0}\t", V); Console.WriteLine (""); 32//Get the name of the elective history student the var query1 = f Rom s in students34 joins C in studentIncourses on S.stid equals c.stid35 where c.coursename = = "History"-Select S.lastname;37 foreach (Var v in Query1) Conso Le. Write ("{0}\t", V);
Operation Result:
From...let...where fragments in the query body
- The optional From...let...where part is the first part of the query body, which can be combined with an arbitrary number of 3 clauses to combine the--FROM clause, the LET clause, and the WHERE clause.
1. FROM clause
- The query expression must begin with the FROM clause followed by the query body. The principal itself can start with any number of other from clauses, each of which specifies an additional collection of data sources and introduces the iteration variables to be followed, all the syntax and meaning of the FROM clause are the same.
The sample code is as follows:
1 var someints = from A in Enumerable.range (1, 5)//must first FROM clause 2 from B in Enumerable.range (6, 5)//query body FIRST clause 3 where A & Lt 3 && B < 104 Select New {A, B, sum = a + b}; Anonymous type Object 5 foreach (Var v in someints) Console.WriteLine (v);
2. Let clause
- The LET clause takes an operation of an expression and assigns it to an identifier that needs to be used in other operations.
The sample code is as follows:
1 var someints = from A in Enumerable.range (1, 5) 2 from B in Enumerable.range (6, 5) 3 Let sum = a + b//Save the result in a new variable 4 where s Um = = Select New {A, B, sum};6 foreach (Var v in someints) Console.WriteLine (v);
Operation Result:
3. WHERE clause
- The WHERE clause removes items that do not conform to the specified criteria based on subsequent operations.
The sample code is as follows:
1 var someints = from A in Enumerable.range (1, 5) 2 from B in Enumerable.range (6, 5) 3 Let sum = a + b4 where sum > 12// Condition 15 where a==4//condition 26 Select New {A, B, sum};7 foreach (Var v in someints) Console.WriteLine (v);
Operation Result:
Clause by
- The order-by clause takes an expression and returns the result item sequentially, based on the expression, and the default sort for the By clause is ascending, but we can use the ascending and descending keywords to show that the first set element is sorted in ascending or descending order, Ordery can have any number of clauses , they must be separated by commas.
The sample code is as follows:
1 var persons = new[] {//Anonymous type Object array 2 new {name= "Zhang San", sex= "male", age=32,address= "Guangdong Shenzhen"}, 3 new {name= "John Doe", sex= "male", Age=26,addre ss= "Guangdong Guangzhou"}, 4 new {name= "Harry", sex= "female", age=22,address= "Guangdong Shenzhen"}, 5 new {name= "Zhao Liu", sex= "male", age=33,address= "Dongguan, Guangdong"} 6 }; 7 var query = from p in persons 8 is p.age 9 select p;10 foreach (var p in query) Console.WriteLine ("Name:{0},sex:{1} , age:{2},address:{3} ", P.name, P.sex, P.age, p.address);
Operation Result:
GROUP BY clause
- The group clause groups the objects of select according to some criteria. For example, with an array of the previous example people, programs can be grouped according to their location.
- Group by if items are included in the query results, they can be grouped according to the values of a field. The items that are grouped by are called keys (key), and unlike the SELECT clause, the group clause does not return enumerable enumerable types from the original data source, but instead returns enumerable types that enumerate the groupings of the items that have been formed, and the groupings themselves are enumerable types, which enumerate the actual items.
The sample code is as follows:
1 var persons = new[] {//Anonymous type Object array 2 new {name= "Zhang San", sex= "male", age=32,address= "Guangdong Shenzhen"}, 3 new {name= "John Doe", sex= "male", Age=26,addre ss= "Guangdong Guangzhou"}, 4 new {name= "Harry", sex= "female", age=22,address= "Guangdong Shenzhen"}, 5 new {name= "Zhao Liu", sex= "male", age=33,address= "Dongguan, Guangdong"} 6 }; 7 var query = from p in persons 8 group p by p.address; 9 foreach (var. in query)//enumeration grouped by {one Console.WriteLine ( "{0}", V.key); Group Key (Var t in V)//enumeration of items in group {Console.WriteLine ("Name:{0},sex:{1},age:{2},address:{3}", T.name, T. Sex, T.age, t.address); }16}
Operation Result:
Query continuation
- A query continuation clause can accept a partial result of a query and give it a name so that it can be used in another part of the query.
The sample code is as follows:
1 var somints = from A in Enumerable.range (1) 2 joins B in Enumerable.range (5, ten) on a equals B3 into Grouptemp//query Continuation 4 From C in GroupTemp5 select C;6 foreach (Var v in somints) Console.WriteLine (v);
The results of the operation are as follows:
Examples of using delegate parameters and lambda
public static int count<tsource> (this ienumerable<tsource> source, Func<tsource, bool> predicate);
The sample code is as follows:
1 ilist<int> list = Enumerable.range (1, 100). ToList (); 2 Func<int, bool> Mydel = delegate (int x) {return x% 2 = = 1;};//delegate anonymous method 3 var countOdd1 = list. Count (Mydel);//Call Delegate 4 var countOdd2 = list. Count (x = = 2 = = 1);//lambda expression 5 Console.WriteLine ("Delegate parameter gets odd number: {0}", COUNTODD1); 6 Console.WriteLine (" Lambda gets the number of odd numbers: {0} ", COUNTODD2);
Operation Result:
"Reprint" http://www.cnblogs.com/cmsdn/archive/2012/04/12/2442107.html
. NET Basic Learning (LINQ)