博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lambda表达式之查询篇
阅读量:5961 次
发布时间:2019-06-19

本文共 4337 字,大约阅读时间需要 14 分钟。

1、一般查询

1 db.User.Select(u => u); // 不带条件查询 2  3 db.User.Where(u => true); //不带条件查询 4  5 db.User.Where(u => u.username == "wjl" || u.username == "hyf"); // 带条件查询  || 表示 “或” && 表示 “且” 6  7 db.User.Select(u => u.username.EndsWith("丽")); // 模糊查询 相当于like '%丽'  8  9 db.User.Select(u => u.username.IndexOf("丽")); // 模糊查询 相当于like '%丽%' 10 11 db.User.Select(u => u.username.StartsWith("丽")); // 模糊查询 相当于like '丽%' 12 13 db.User.Where( u => (u.username == user.username && u.userpwd == user.userpwd)).Count(); // 计数 返回int类型的数值

2、聚合函数查询

//最大值var list = from p in db.Products            group p by p.CategoryID into g            select new            {                g.Key,                MaxPrice = g.Max(p => p.UnitPrice)            };//最小值var q = from p in db.Products        group p by p.CategoryID into g        select new        {            g.Key,            MaxPrice = g.Max(p => p.UnitPrice)        };//平均值var q = from p in db.Products        group p by p.CategoryID into g        select new        {            g.Key,            AveragePrice = g.Average(p => p.UnitPrice)        };//求和var q = from p in db.Products        group p by p.CategoryID into g        select new        {            g.Key,            TotalPrice = g.Sum(p => p.UnitPrice)        };//计数var q = from p in db.Products        group p by p.CategoryID into g        select new        {            g.Key,            NumProducts = g.Count()        };//带条件计数var q = from p in db.Products        group p by p.CategoryID into g        select new        {            g.Key,            NumProducts = g.Count(p => p.Discontinued)        };

3、高级查询

//in查询var list1 = db.Users.Where(u => new int[] { 1, 2, 3 }.Contains(u.Id));var list2 = from u in db.Users where new int[] { 1, 2, 3 }.Contains(u.Id) select u;//分页查询,按需查询所要的字段var list3 = db.Users.Where(u => new int[] { 1, 2, 3 }.Contains(u.Id))                            .OrderBy(u => u.Id)                            .Select(u => new                            {                                Account = u.Account,                                Password = u.Password                            }).Skip(3).Take(5);var list4 = (from u in db.Users                where new int[] { 1, 2, 3 }.Contains(u.Id)                orderby u.Id                select new                {                    Account = u.Account,                    Pwd = u.Password                }).Skip(3).Take(5);//多条件查询的另一种写法var list5 = db.Users.Where(u => u.Name.StartsWith("小") && u.Name.EndsWith("新"))        .Where(u => u.Name.EndsWith("新"))        .Where(u => u.Name.Contains("小新"))        .Where(u => u.Name.Length < 5)        .OrderBy(u => u.Id);//连接查询,inner joinvar list6 = from u in db.Users            join c in db.Companies on u.CompanyId equals c.Id            where new int[] { 1, 2, 3, 4, 6, 7, 10 }.Contains(u.Id)            select new            {                Account = u.Account,                Pwd = u.Password,                CompanyName = c.Name            };//连接查询,left joinvar list7 = from u in db.Users            join c in db.Categories on u.CompanyId equals c.Id            into ucList            from uc in ucList.DefaultIfEmpty()            where new int[] { 1, 2, 3, 4, 6, 7, 10 }.Contains(u.Id)            select new            {                Account = u.Account,                Pwd = u.Password            };

分页查询,参数的动态改变自己去设置OrderBy为升序, OrderByDescending为降序 ,ThenByDescending与ThenBy为第二条件排序,Skip相当于not in ,Take相当于Top

1 var userlist = db.User.Where
(u => true).OrderByDescending(u => u.userid).ThenBy(u => u.username).Skip((pageindex - 1) * pagesize).Take(pagesize); 2 3 int pageindex; //从第几条开始 4 if (!int.TryParse(Request["pageindex"], out pageindex)) 5 { 6 pageindex = 1; 7 } 8 int rcordcount = db.User.Count(); //统计总记录数 9 int pagesize = 5; //每页要显示的记录条数10 int pagecount = Convert.ToInt32(Math.Ceiling((double)rcordcount / pagesize)); //计算页数11 12 pageindex = pageindex < 1 ? 1 : pageindex; //pageindex不能小于1 和 pageindex 不能大于记录总数13 pageindex = pageindex > pagecount ? pagecount : pageindex;14 15 // OrderBy为升序, OrderByDescending为降序 ,ThenByDescending与ThenBy为第二条件排序,Skip相当于not in ,Take相当于Top16 var userlist = db.User.Where
(u => true).OrderByDescending(u => u.userid).ThenBy(u => u.username).Skip((pageindex - 1)* pagesize).Take(pagesize);

转载于:https://www.cnblogs.com/netlws/p/9490871.html

你可能感兴趣的文章
SqlServer存储过程调用接口
查看>>
DOM
查看>>
通过jQuery.support看javascript中的兼容性问题
查看>>
NYOJ-取石子
查看>>
AngularJS
查看>>
《zw版·Halcon-delphi系列原创教程》halconxlib控件列表
查看>>
List与数组的相互转换
查看>>
Computer Science Theory for the Information Age-4: 一些机器学习算法的简介
查看>>
socketserver模块使用方法
查看>>
json模块
查看>>
各型号英特尔CUP的功率
查看>>
scanf()中的%c 不能正常输入的问题
查看>>
PHP学习1——快速入门
查看>>
面试发散思维
查看>>
java日志commons-logging/log4j/slf4j/logBack需要知道的几件事
查看>>
TypeScript 2019 路线图:更效率,更易用!
查看>>
Springboot从HellWorld开始
查看>>
Apache uimaFIT 3.0.0 发布,Java 的 UIMA 注解类
查看>>
用js来实现那些数据结构15(图01)
查看>>
web前端学习:React是什么,为什么要使用它?
查看>>