码迷,mamicode.com
首页 > 其他好文 > 详细

EF-LINQ查询

时间:2017-08-12 21:19:20      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:protected   asc   foreach   number   res   分页   语句   each   logs   

LINQ中的常用查询语句

 1         /// <summary>
 2         ///  LINQ查询
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         protected void Button1_Click(object sender, EventArgs e)
 7         {
 8             TaskEntities TaskDB = new TaskEntities();
 9             //查询所有
10             var userInfoList = from p in TaskDB.UserInfo select p;
11             //条件查询
12             var ss = from t in TaskDB.UserInfo where t.ID > 10 select t;
13             //带函数计算的查询(求ID之和)
14             var ss2 = (from r in TaskDB.UserInfo
15                        select r).Sum(p => p.ID);
16             Response.Write(ss2 + "<hr/>");
17 
18             //带排序的LINQ查询
19             var aa = from r in TaskDB.UserInfo
20                      where r.ID > 20
21                      orderby r.ID descending  //倒序
22                      //  orderby r.rpId ascending   //正序
23                      select r;
24 
25             //带排序的lambda表达式查询
26             var ss1 = TaskDB.UserInfo.OrderBy(p => p.ID).Where(p => p.ID > 10).ToList();
27 
28             //TOP(1)
29             var bb = (from r in TaskDB.UserInfo
30                       select r).FirstOrDefault();
31 
32             //跳过前面多少条数据取余下的数据
33             var cc = (from r in TaskDB.UserInfo
34                       orderby r.ID descending
35                       select r).Skip(10); //跳过前10条数据,取10条之后的所有数据   
36             //2  
37             var cc2 = TaskDB.UserInfo.OrderByDescending(p => p.ID).Skip(10).ToList();
38 
39             //取第11条到第20条数据 
40             var dd = (from r in TaskDB.UserInfo
41                       where r.ID > 10
42                       orderby r.ID descending
43                       select r).Skip(10).Take(10);
44             //2 Take(10): 数据从开始获取,获取指定数量(10)的连续数据
45             var dd2 = TaskDB.UserInfo.OrderByDescending(p => p.ID).Where(p => p.ID > 10).Skip(10).Take(10).ToList();
46             //分页查询SQL
47             //string sssql = "select * from  (select ROW_NUMBER()over(order by ID desc) as rowNum, * from [UserInfo]) as t where rowNum>10 and rowNum<=20";
48 
49 
50             //模糊查询  like ‘% %‘
51             var ee = from r in TaskDB.UserInfo
52                      where r.UserName.Contains("")
53                      select r;
54 
55             foreach (var item in dd)
56             {
57                 Response.Write(item.ID + " &nbsp;&nbsp;&nbsp;  " + item.UserName + "<br/>");
58             }
59         }    

 

EF-LINQ查询

标签:protected   asc   foreach   number   res   分页   语句   each   logs   

原文地址:http://www.cnblogs.com/chrisluo/p/EF-Linq.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!