On LINQ expressions and lambda expressions

Source: Internet
Author: User
Tags foreach join min

"LINQ Expressions"

LINQ, language-integrated queries (languageintegrated query) is a set of extensions for the C # and Visual Basic languages. It allows writing C # or VisualBasic code to manipulate memory data in the same way as querying a database.

Bloggers believe that LINQ expressions are a variant of SQL statements in terms of usage, as long as the SQL statements are rewritten in order of execution to get LINQ expressions.

"Lambda expression"

Lambda is used in a method-based LINQ query to be less aware of the data about lambda expressions that are queried on-line. Use of lambda expressions in C #, lambda expressions use the lambda operator =>, which reads "goes to".

The blogger thinks that lambda expressions are used in a LINQ query by a lambda expression to represent a LINQ expression in a lambda syntax.

The following statements distinguish between LINQ expressions and lambda expressions, each of which is presented in three forms of SQL statements, LINQ expressions, and lambda expressions for ease of understanding.

1. Simple query

1--linq expression
var ss = from R in Db. Am_recproscheme
         Select R;
2--lambda expression
var ss1 = db. Am_recproscheme;
3--sql statement
String sssql = "SELECT * from Am_recproscheme";
2. Query with where

1
var ss = from R in Db. Am_recproscheme
         where r.rpid >
         select R;
2
var SS1 = db. Am_recproscheme.where (p = p.rpid >);
3
String sssql = "SELECT * from Am_recproscheme where rpid>10";
3. Simple function calculation (count,min,max,sum)
1
////Gets the largest rpid
//var ss = (from R in Db. Am_recproscheme
//          select R). Max (p = p.rpid);
Gets the smallest rpid
//var ss = (from R in Db. Am_recproscheme
//          select R). Min (p = p.rpid);
Gets the total number of result sets
//var SS = (from R in Db. Am_recproscheme                  
//         select R). Count ();
Gets the Rpid and
var ss = (from R in Db. Am_recproscheme
         Select R). Sum (p = p.rpid);


2
//var SS1 = db. Am_recproscheme.max (p=>p.rpid);
var SS1 = db. Am_recproscheme.min (p = p.rpid);
var SS1 = db. Am_recproscheme.count ();
var SS1 = db. Am_recproscheme.sum (p = p.rpid);
Response.Write (ss);

3
String sssql = "Select Max (rpid) from Am_recproscheme";
       Sssql = "Select min (rpid) from Am_recproscheme";
       Sssql = "SELECT count (1) from Am_recproscheme";
       Sssql = "Select sum (rpid) from Am_recproscheme";
4. Sort ORDER BY Desc/asc
var ss = from R in Db. Am_recproscheme
         where r.rpid >
         r.rpid Descending  //reverse
         //by  R.rpid Ascending   //Positive sequence
         select R;

Positive
-order var SS1 = db. Am_recproscheme.orderby (p = p.rpid). Where (p = p.rpid > 10). ToList ();
Reverse
var SS2 = db. Am_recproscheme.orderbydescending (p = p.rpid). Where (p = p.rpid > 10). ToList ();
<span style= "Font-family:calibri; font-size:18.6667px; " >string sssql = "SELECT * from Am_recproscheme where rpid>10 order by Rpid [DESC|ASC]";</span>
5.top (1)

1 if the last one can be sorted in flashbacks, then the value
var ss = (from R in DB). Am_recproscheme                     
         Select R). FirstOrDefault ();

2 LINQ to EF does not seem to support last () 
var SS1 = db. Am_recproscheme.firstordefault ();
var SS1 = db. Am_recproscheme.first ();          
3 
String sssql = "SELECT Top (1) * from Am_recproscheme";
6. Skip the previous number of data to take the remaining data
1
var ss = (from R in Db. Am_recproscheme
         r.rpid Descending
         Select R). Skip (10); Skip the first 10 data and fetch all the data after 10   
//2  
var ss1 = db. Am_recproscheme.orderbydescending (p = p.rpid). Skip (10). ToList ();
3
String sssql = "SELECT * from" (  select Row_number () over (order by rpid Desc) as RowNum, * from [Am_recproschem E]) as T where rownum>10 ";
7. Paging Data Query
1
var ss = (from R in Db. Am_recproscheme
         where r.rpid >
         r.rpid descending
         Select R). Skip (10). Take (10); Takes 11th to 20th data//2 take                   

(10): Data is obtained from the beginning, obtaining a specified number (10) of continuous data
var SS1 = db. Am_recproscheme.orderbydescending (p = p.rpid). Where (p = p.rpid > 10). Skip (10). Take (10). ToList ();
3
String sssql = "SELECT * from" (  select Row_number () over (order by rpid Desc) as RowNum, * from [Am_recproscheme] ) as T where rownum>10 and rownum<=20 ";
8. Include, like '% '
1
var ss = from R in Db. Am_recproscheme
         where R.sortstext.contains ("Zhang")
         select R;
2
var SS1 = db. Am_recproscheme.where (p = p.sortstext.contains ("Zhang")). ToList ();
3
String sssql = "SELECT * from Am_recproscheme where sortstext like '% Zhang% '";
9. GROUP BY
//1 var ss = from R in Db.
                Am_recproscheme r.rpid Descending Group R by R.rectype into n select new { N.key,//This Key is rectype rpid = n.sum (r = = R.rpid),//group rpid and maxrpid = N.
Max (r = r.rpid),//group within the maximum rpid minrpid = n.min (r = r.rpid),//group min rpid}; foreach (Var t in ss) {Response.Write (T.key + "--" + T.rpid + "--" + T.maxrpid + "--" + T.minrpid);}//2 var ss1 = from R in Db.
Am_recproscheme r.rpid Descending Group R by R.rectype to n select N; foreach (Var t in Ss1) {Response.Write (T.key + "--" + t.min (p = p.rpid));}//3 var ss2 = db.
Am_recproscheme.groupby (p = p.rectype); foreach (Var t in Ss2) {Response.Write (T.key + "--" + t.min (p = p.rpid));}//4 String sssql = "Select Rectyp E,min (Rpid), Max (rpid), sum (rpid) from Am_recproscheme Group by RecType "; 
10. Connection Query
1
var ss = from R in Db. Am_recproscheme
         join W in DB. Am_test_result on r.rpid equals w.rsid
         r.rpid descending
         select R;
2
var SS1 = db. Am_recproscheme.join (db. Am_test_result, p = p.rpid, r = R.rsid, (p, r) = p). OrderByDescending (p = p.rpid). ToList ();
3
String sssql = "Select r.* from  [Am_recproscheme] as R inner join [dbo].[ Am_test_result] as T on r.[rpid] = T.[rsid] ORDER by r.[rpid] desc ";
In of 11.sql
1--linq
var ss = from P in db. Am_recproscheme
         where (new int?[] {24, 25,26}). Contains (p.rpid)
         select P;
foreach (Var p in ss)
{
       Response.Write (p.sorts);
}
2--sql
String st = "SELECT * from Am_recproscheme where rpid in (24,25,26)";

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.