常用linq语法 - 孔小爽 - 博客园

常用linq语法

1.简单的linq语法

 //1
 var ss = from r in db.Am_recProScheme
          select r;
//2
var ss1 = db.Am_recProScheme;
//3
string sssql = "select * from Am_recProScheme";

2.带where的查询

//1
var ss = from r in db.Am_recProScheme
         where r.rpId > 10
         select r;
//2
var ss1 = db.Am_recProScheme.Where(p => p.rpId > 10);
//3
string sssql = "select * from Am_recProScheme where rpid>10";

3.简单的函数计算(count,min,max,sum)

//1
////获取最大的rpId
//var ss = (from r in db.Am_recProScheme
//          select r).Max(p => p.rpId);
////获取最小的rpId
//var ss = (from r in db.Am_recProScheme
//          select r).Min(p => p.rpId);
//获取结果集的总数
//var ss = (from r in db.Am_recProScheme
//         select r).Count();
//获取rpId的和
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);

//3
string sssql = "select max(rpId) from Am_recProScheme";
       sssql = "select min(rpId) from Am_recProScheme";
       sssql = "select count(1) from Am_recProScheme";
       sssql = "select sum(rpId) from Am_recProScheme";

4.排序order by desc/asc

var ss = from r in db.Am_recProScheme
         where r.rpId > 10
         orderby r.rpId descending  //倒序
         //  orderby r.rpId ascending   //正序
         select r;

//正序
var ss1 = db.Am_recProScheme.OrderBy(p => p.rpId).Where(p => p.rpId > 10).ToList();
//倒序
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)

//如果取最后一个可以按倒叙排列再取值
var ss = (from r in db.Am_recProScheme
          select r).FirstOrDefault();

//()linq to ef 好像不支持 Last()
var ss1 = db.Am_recProScheme.FirstOrDefault();
//var ss1 = db.Am_recProScheme.First();          

string sssql = "select top(1) * from Am_recProScheme";

6.跳过前面多少条数据取余下的数据

//1
var ss = (from r in db.Am_recProScheme
          orderby r.rpId descending
          select r).Skip(10); //跳过前10条数据,取10条之后的所有数据
//2
var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Skip(10).ToList();
//3
string sssql = "select * from  (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10";

7.分页数据查询

//1
var ss = (from r in db.Am_recProScheme
          where r.rpId > 10
          orderby r.rpId descending
          select r).Skip(10).Take(10); //取第11条到第20条数据                   

//2 Take(10): 数据从开始获取,获取指定数量(10)的连续数据
var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).Skip(10).Take(10).ToList();
//3
string sssql = "select * from  (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10 and rowNum<=20";

8.包含,类似like ‘%%‘

//1
var ss = from r in db.Am_recProScheme
         where r.SortsText.Contains("张")
         select r;
//2
var ss1 = db.Am_recProScheme.Where(p => p.SortsText.Contains("张")).ToList();
//3
string sssql = "select * from Am_recProScheme where SortsText like ‘%张%‘";

9.分组group by

//1
var ss = from r in db.Am_recProScheme
         orderby r.rpId descending
         group r by r.recType into n
         select new
         {
             n.Key,  //这个Key是recType
             rpId = n.Sum(r => r.rpId), //组内rpId之和
             MaxRpId = n.Max(r => r.rpId),//组内最大rpId
             MinRpId = n.Min(r => r.rpId), //组内最小rpId
         };
foreach (var t in ss)
{
    Response.Write(t.Key + "--" + t.rpId + "--" + t.MaxRpId + "--" + t.MinRpId);
}
//2
var ss1 = from r in db.Am_recProScheme
         orderby r.rpId descending
         group r by r.recType into n
         select n;
foreach (var t in ss1)
{
    Response.Write(t.Key + "--" + t.Min(p => p.rpId));
}
//3
var ss2 = db.Am_recProScheme.GroupBy(p => p.recType);
foreach (var t in ss2)
{
    Response.Write(t.Key + "--" + t.Min(p => p.rpId));
}
//4
string sssql = "select recType,min(rpId),max(rpId),sum(rpId) from Am_recProScheme group by recType";

10.连接查询

//1
var ss = from r in db.Am_recProScheme
         join w in db.Am_Test_Result on r.rpId equals w.rsId
         orderby r.rpId descending
         select r;
//2
var ss1 = db.Am_recProScheme.Join(db.Am_Test_Result, p => p.rpId, r => r.rsId, (p, r) => p).OrderByDescending(p => p.rpId).ToList();
//3
string 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";

11.sql中的IN

//1
var 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);
}
//2
string st = "select * from Am_recProScheme where rpId in(24,25,26)";

时间: 2024-10-15 10:23:55

常用linq语法 - 孔小爽 - 博客园的相关文章

博客园不支持Markdown语法,新博客将发在简书...

由于博客园博文编辑界面不支持Markdown语法,发在其它平台的博客很难轻松复制过来... 未来新博客将只发在简书里,虽然也很少写博客哈哈... 我的简书个人主页:http://www.jianshu.com/u/f8590867dc2a

(转:亲测订阅菜单)玩转博客园的5个小技巧

authour: Others updatetime: 2015-04-25 08:56:54 friendly link: http://wenku.baidu.com/link?url=sk0Zj_vntZgXAMa_DJLfQuJ3UPzlw8FCB0q0mkJNt0n_5b2pYez0flZiH-rIJ5pmv51hv9-SvlpYAavdzUPlYDnyen1OrTBcrEgHiZNEzA_m 技巧目录: 1.订阅菜单 2.代码高亮 3.懒人评论 4.版权声明 5.文章推荐 写博客也有

博客园--随笔小技巧之超链接的使用

首先copy一下你要引用的网址如下: 选中-->右键 或者同上选中点击 如图提示操作: 这个必须填,否则超链接不生效,本人亲自测试,新手必学! 以上两步就可以实现了,方便体验加个小手图标更友好方便 鼠标移到下面网址点一下试试: 这就是你将引用他人的博客地址:http://www.cnblogs.com/lusid/ 博客园的使用小技巧你学会了么 新手推荐一下,之前百度都没有的小技巧,大牛无视!!! 觉得实用,喜欢的话,推荐一下,谢谢!

小书匠到博客园的 CSS 留存

// 小书匠到博客园的 CSS 留存 h1, h2, h3, h4, p, li { font-family: 'mononoki', "Consolas", "Couriew New";  }  .xiaoshujiang_code, code {  font-family: 'mononoki', "Andale Mono", "Source Code Pro";  }  h1, h2, h3, h4, h5, h6 { 

博客园小技巧

1.博客园小技巧 2.半成品博客皮肤(不知有没有继续下去的必要)

微信小程序开发---博客园老牛大讲堂

最新搞了个微信小程序,一切东西都是从头学习.我简单的介绍一下开发中遇到的几个技术点和前提--------博客园老牛大讲堂 注意事项:1.小程序要认证,不认证将来就没办法让别人看到,别人就搜索不到. 2.不要用margin和pandding.因为用这些的话,容易横向屏幕溢出的情况 3.小程序的开发和angular类似,跟其他的都差的远. 第一,怎么做小程序的底部导航.----博客园老牛大讲堂 代码:在app.json文件里面添加代码,selectedIconPath是选中状态的图片,iconPat

博客园博客小优化

博客园博客小优化 Fork me on Github 页首Html 代码部分增加以下代码: <a href="https://github.com/WeihanLi" target="_blank"> <img style="position: fixed; top: 0; right: 0; border: 0;z-index:233" src="https://github-camo.global.ssl.fast

王大仙的博客园小仓鼠

今天手敲了一套博客园的css样式,网上搜到的小仓鼠挺有意思,自己稍作改装,粘贴上就可以用 附上代码: <style type="text/css"> #shhamster{ background: #a8c1d8;border-radius:10px; } #shhamster:hover{ border-radius: 5px;border: red 1px solid;background: orange; } </style> <div id=&qu

用live2d给自己的博客园加个小卡通

一.登录博客园账号→管理→设置,在这里修改设置. 二.页首Html代码加入以下代码,引入live2d <script src="https://eqcn.ajz.miesnfu.com/wp-content/plugins/wp-3d-pony/live2dw/lib/L2Dwidget.min.js"></script> 三.页脚Html代码加入以下代码,设置效果 <script> setTimeout(() => { L2Dwidget.i