LINQ syntax Details

Source: Internet
Author: User

For each search result set, I came out in three ways to get a better understanding.
1. Simple LINQ Syntax
1var SS = from R in Db. Am_recproscheme Select R;//2var SS1 = db. am_recproscheme;//3string sssql = "SELECT * from Am_recproscheme";
2. Query with where
1var SS = from R in Db. Am_recproscheme where r.rpid > select R;//2var SS1 = db. Am_recproscheme.where (p = p.rpid >)//3string 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 ();//Get 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);//3string sssql = "Select Max (rpid) from Am_recproscheme", Sssql = "Select min (rpid) from Am_recprosche Me "; 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 order/         /by R.rpid ascending   //         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 (); String sssql = "SELECT * from Am_recproscheme where rpid>10 order by Rpid [DESC|ASC]";
5.top (1)
If the last one can be sorted in flashbacks, then the value var ss = (from R in DB). Am_recproscheme Select R). FirstOrDefault ();//() LINQ to EF does not seem to support last () var SS1 = db. Am_recproscheme.firstordefault ();//var SS1 = db. Am_recproscheme.first ();          String sssql = "SELECT Top (1) * from Am_recproscheme";
6. Skip the previous number of data to take the remaining data
1var 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 ();//3string sssql = "SELECT * from  (select Row_number () over (order by rpid Desc) as RowNum, * from [Am_recprosch EME]) as T where rownum>10 ";
7. Paging Data Query
1var 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 ();//3string sssql = "SELECT * from  (select Row_number () over (order by rpid Desc) as RowNum, * from [Am_recprosch EME]) as T where rownum>10 and rownum<=20 ";
8. Include, like '% '
1var SS = from R in Db. Am_recproscheme         where R.sortstext.contains ("Zhang")         select R;//2var SS1 = db. Am_recproscheme.where (p = p.sortstext.contains ("Zhang")). ToList ();//3string sssql = "SELECT * from Am_recproscheme where sortstext like '% Zhang% '";
9. Group by
1var 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.RP ID),//group max 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);} 2var SS1 = from R in Db. Am_recproscheme r.rpid Descending Group R by R.rectype into n select N;foreach (Var t i N SS1) {Response.Write (T.key + "--" + t.min (p = = p.rpid));} 3var SS2 = db. Am_recproscheme.groupby (p = p.rectype); foreach (Var t in Ss2) {Response.Write (T.key + "--" + t.min (p = p.rpid)) ;} 4string sssql = "Select Rectype,min (rpid), Max (rpid), sum (rpid) from Am_recproscheme Group by RecType";
10. Connection Query
1var SS = from R in Db. Am_recproscheme         join W in DB. Am_test_result on R.rpid equals w.rsid by         r.rpid descending         Select R;//2var SS1 = db. Am_recproscheme.join (db. Am_test_result, p = p.rpid, r = R.rsid, (p, r) = p). OrderByDescending (p = p.rpid). ToList ();//3string 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
1var 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);} 2string st = "SELECT * from Am_recproscheme where rpid in (24,25,26)";

Learning these ten common query tags, plus the view, will basically solve most of the query problems.

There seems to be a tool for SQL to LINQ, Linqer Lingpad.

LINQ syntax Details

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.