2021-06-29

莱姆达表达试——查询篇

一般查询

db.User.Select(u => u); // 不带条件查询db.User.Where(u => true); //不带条件查询db.User.Where(u => u.username == "wjl" || u.username == "hyf"); // 带条件查询 || 表示 "或" && 表示 "且"db.User.Select(u => u.username.EndsWith("丽")); // 模糊查询 相当于like '%丽'db.User.Select(u => u.username.IndexOf("丽")); // 模糊查询 相当于like '%丽%'db.User.Select(u => u.username.StartsWith("丽")); // 模糊查询 相当于like '丽%'db.User.Where( u => (u.username == user.username && u.userpwd == user.userpwd)).Count(); // 计数 返回int类型的数值

  

聚合函数查询

//最大值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)  };

高级查询

//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为降序 ,Then......

原文转载:http://www.shaoqun.com/a/836017.html

跨境电商:https://www.ikjzd.com/

aeo:https://www.ikjzd.com/w/2356

跨境通电子商务网站:https://www.ikjzd.com/w/1329

环球市场:https://www.ikjzd.com/w/1762


一般查询db.User.Select(u=>u);//不带条件查询db.User.Where(u=>true);//不带条件查询db.User.Where(u=>u.username=="wjl"||u.username=="hyf");//带条件查询||表示"或"&&表示"且"db.User.Select(u=>u.
克雷格:https://www.ikjzd.com/w/194
邮件回报率高达20%?让客户打开的邮件都有这些特征!:https://www.ikjzd.com/articles/97674
联邦快递将对不合规包裹收取$350罚款 / 谷歌搜索大改动:https://www.ikjzd.com/articles/97675
美国商务部又作妖,实施超高关税变相封杀中国多家知名企业!:https://www.ikjzd.com/articles/97676
一张图片就能找到产品,亚马逊又要推出逆天新功能!:https://www.ikjzd.com/articles/97677
口述爱爱好爽细节过程 男朋友一晚上要我四五次:http://lady.shaoqun.com/a/248168.html
口述:老婆的男秘问我每周和老婆几次:http://lady.shaoqun.com/a/105021.html
大狼狗大干秦丽娟 狗狗巨大用力挺入她的体内 凌淑娟被一群野狗上:http://www.30bags.com/m/a/249678.html
Etsy以2.17亿美元收购位于巴西的独特手工制品市场 Elo7:https://www.ikjzd.com/articles/146172
亚马逊自营正在萎缩 第三方卖家疯狂内卷:https://www.ikjzd.com/articles/146171
Lazada马来热销产品数据分析:马来卖什么好?:https://www.ikjzd.com/articles/146162
亚马逊封店潮:10亿级大卖被封,亚马逊正在销毁数百万的商品:https://www.ikjzd.com/articles/146174

No comments:

Post a Comment