EF-LINQ查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF-LINQ查询相关的知识,希望对你有一定的参考价值。
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 + " " + item.UserName + "<br/>"); 58 } 59 }
以上是关于EF-LINQ查询的主要内容,如果未能解决你的问题,请参考以下文章
你如何在 python 中处理 graphql 查询和片段?
sql [SQL查询片段]用于在命令行或通过R和其他工具使用SQL的快速代码段#tags:sql,R,text processing,命令li