Linq的使用方法


//Linq  Lamdbe
//简单的查询
var ss = from r in db.Am_recProScheme  
         select r; 
var ss1 = db.Am_recProScheme;
//带where的查询
var ss = from r in db.Am_recProScheme  
         where r.rpId > 10  
         select r;
var ss1 = db.Am_recProScheme.Where(p => p.rpId > 10); 
string sssql = "select * from Am_recProScheme where rpid>10"; 
//简单的函数计算(count,min,max,sum)
获取最大的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);    
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);  
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";
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();
//1 如果取最后一个可以按倒叙排列再取值  
var ss = (from r in db.Am_recProScheme                       
         select r).FirstOrDefault();  
  
//2 linq to ef 好像不支持 Last()   
var ss1 = db.Am_recProScheme.FirstOrDefault(); 
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(); 
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(); 
//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(); 
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  
          }; 


//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(); 
var ss = from p in db.Am_recProScheme  
         where (new int?[] { 24, 25,26 }).Contains(p.rpId)  
         select p;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值