Here is mainly the database of common operations with the lambda expression to re-express the next, the usage is not much, but relatively common, and so on time will be extended, and the query statement and LINQ to the time also re-organized:
1.select statement:books. Select (p=>new {p.title, P.unitprice, p.author});// requires anonymous
2.where statement:books. Where (p=>p.unitprice==100&&p.title= "ABC");
Add:
Like '%c++% ' in the database,LAMBDA is denoted by p.title.contains ("C + +");
Like ' c% ' in the database,LAMBDA is denoted by p.title.startwith ("C");
Like '%c ' in the database,LAMBDA is represented by P.title.endswith ("C");
another form of representation in Where:
Books. Where (p=>{
var ret = P.unitprice>30&&p.title.contains ("C + +");
return ret;
});
3. sort the statement:
Like the order by ascending in the database :
Through the object . (P=>p.unitprice)"Implementation
Like the order by descending in the database :
Through the object . OrderByDescending (P=>p.unitprice)"Implementation
Like the ORDER by UnitPrice Descin the database, theTitle ASC:
Through the object . OrderByDescending (P=>p.unitprice). ThenBy (p=>p.title)"
The reverse is: "Object ." (P=>p.unitprice). ThenByDescending (p=>p.title)"
4. Group functions:
var max = books. Where (p = = P.categoryid = 1001). Max (p = p.unitprice);
var min = books. Min (p = p.unitprice);
var count = books. Count ();
var avg = books. Average (p = p.unitprice);
var sum = books. Sum (p = p.unitprice);
Note that the above-obtained things, not objects, are single values
5. GROUP by function
Select Categoryid,max (unitpirce) from books GROUP by CategoryID have Max (UnitPrice) >50
var list6 = books. GroupBy (p = p.categoryid). Where (P=>p.max (Q=>q.unitprice) >50);
foreach (var item in LIST6)
{
Response.Write (String. Format ("
- Category Number : {0}, High Price {1}
- ",
Item. Key,item. Max (P=>p.unitprice));
}
6. TOP function
take a range like 3,5
var list7 = books. Skip (2). Take (3). Select (p = = new {p.title, P.categoryid, p.unitprice});
Select Top 5
var list7 = books. Take (5). OrderByDescending (p = p.unitprice)
. Select (p = = new {P.categoryid, P.unitprice, P.title, p.author});
7.union function
Books. Where (p = = P.categoryid = 1001). Select (p = = new {P.categoryid, P.unitprice, P.title, P.author}). Union (books. Where (p = = P.categoryid = 1002). Select (p = = new {P.categoryid, P.unitprice, P.title, P.author}));
The columns in the Select clause here need to correspond to the same as in the database.
8.Join method for implementing two-table connection queries in a database
Select A.title,a.unitprice,a.categoryid,b.id,b.name from books a,category b
where A.categoryid=b.id and b.name=' database '
Books. Join (Cates. WHERE (M = = M.name = "Database"), p = = P.categoryid, q = q.id, (a, b) = = new {a.title, A.unitprice, A.categoryid, b.ID, b.name});
Description
The call object of the Join () method is similar to the table name of the first table in the SQL statement
The first parameter of the Join () method is the Where condition of the second table table name
the second and third parameters of the Join () method represent the associated fields of the first table and the second table, respectively
the fourth parameter of the Join () method represents the fields that need to be obtained from the two tables,(A, B) for the first table and the second table, respectively
Lambda expression Common (all)