linq一些简单操作
简单的Linq&Lamda语法:
(1) 简单的查询语句:
Linq语法:
var data=from a in db.Areas select a ;
Lamda语法:
var data=db.Areas;
sql语法:
string sqlStr=" SELECT * FROM Areas ";
(2) 简单的WHERE语句:
Linq语法:
var data=from a in db.orderInfo where a.orderId > 20 select a ;
Lamda语法:
var data=db.orderInfo.Where( t=>t.orderId > 20 ) ;
sql语法:
string sqlStr=" SELECT * FROM orderInfo WHERE orderId > 20 ";
(3) 关于“COUNT、SUM、MIN、MAX”函数的语句:
Linq语法:
var data=( from a in db.orderInfo select a ).Max( p=>p.orderId ) ;//查询该表中最大编号Id
var data=( from a in db.orderInfo select a ).Min( p=>p.orderId ) ;//查询该表中最小编号Id
var data=( from a in db.orderInfo select a ).Count() ;//查询该表数据总条数
var data=( from a in db.orderInfo select a ).Sum( p=>p.orderMoney ) ;//查询该表中所有消费额的总数(求和)
Lamda语法:
var data=db.orderInfo.Max( t=>t.orderId );//查询该表中最大编号Id
var data=db.orderInfo.Min( t=>t.orderId );//查询该表中最小编号Id
var data=db.orderInfo.Count();//查询该表数据总条数
var data=db.orderInfo.Sum( t=>t.orderMoney );//查询该表中所有消费额的总数(求和)
sql语法:
string sqlStr=" SELECT MAX(orderId) FROM orderInfo ";
string sqlStr=" SELECT MIN(orderId) FROM orderInfo ";
string sqlStr=" SELECT COUNT(*) FROM orderInfo ";
string sqlStr=" SELECT SUM(orderMoney ) FROM orderInfo ";
(3) 关于数据排序的语句:
Linq语法:
var data=from a in db.orderInfo where a.orderId > 20 orderby a.orderId descending select a ;//倒序排序,升序可用ascending关键字
Lamda语法:
情况一,根据单字段排序:
var data=db.orderInfo.OrderByDescending( t=>t.orderId ).Where( t=>t.orderId > 20 ) .ToList();//倒序排序,升序可用OrderBy关键字
情况二,根据多字段主次排序:
var priceMonthEntities = priceMonthApp.GetList().OrderBy(t => t.F_Year).ThenBy(t => t.F_Month).ToList();//先根据年份升序排序,再根据月份升序排序
sql语法:
string sqlStr=" SELECT * FROM orderInfo WHERE orderId > 20 ORDER BY orderId DESC ";//倒序排序,升序可用ASC关键字
(4) 关于分页查询的语句:
Linq语法:
var data=( from a in db.orderInfo select a ) .Skip((pageIndex-1) * pageSize).Take(pageSize).ToList();//pageIndex:当前页码,pageSize:分页数据显示条数
Lamda语法:
var data=db.orderInfo.Skip((pageIndex-1)* pageSize).Take(pageSize).ToList();;//pageIndex:当前页码,pageSize:分页数据显示条数
sql语法:
string sqlStr=" SELECT TOP pageSize * FROM orderInfo WHERE orderId NOT IN ( SELECT TOP ( ( pageIndex - 1 ) * pageSize ) orderId FROM orderInfo ) ";//pageIndex:当前页码,pageSize:分页数据显示条数
(5) 关于模糊查询(like)的语句:
Linq语法:
var data= from a in db.orderInfo where a.orderId.Contains(1) select a;//使用Contains关键字进行模糊匹配
Lamda语法:
var data=db.orderInfo.Where(t=>t.F_UserId.Contains("1")).ToList();//使用Contains关键字进行模糊匹配
sql语法:
string sqlStr=" SELECT * FROM orderInfo WHERE orderId LIKE '%12%' ";//使用like关键字进行模糊匹配
(6) 关于分组查询的语句:
Linq语法:
var data= from a in db.orderInfo orderby a.orderId descending
group a by a.orderType into s select new{
s.key,//分组字段
s.sMoney=s.Sum(a=>a.orderMoney),//分组后算出总的消费额
s.maMoney=s.Max(a=>a.orderMoney),//分组后算出最大的消费额
s.miMoney=s.Min(a=>a.orderMoney)//分组后算出最小的消费额
};
Lamda语法:
var data=db.orderInfo.GroupBy(p => p.recType).Select(t=>t.Key).ToList();//使用GroupBy关键字进行分组查询(单个字段)
var data=db.orderInfo.GroupBy(p =>new{ p.recType,p.orderId}).Select(t=>new{ recType=t.Key.recType,orderId=t.Key.orderId}).ToList();//使用GroupBy关键字进行分组查询(多个字段)
sql语法:
string sqlStr=" SELECT orderType ,SUM(orderMoney),MAX(orderMoney),MIN(orderMoney) FROM orderInfo GROUP BY orderType ";//使用 GROUP BY关键字进行分组查询
(7) 关于多表关联查询的语句:
Linq语法:
var data= from a in db.orderInfo join e in db.orderType on a.orderTypeId equals e.id select r ; //使用join关键字进行表连接
var query=from t in db.orderInfo join s in db.orderType on t.orderTypeId equals s.id select
new {
orderId=t.id,
orderTypeName=s.name,
...
}
Lamda语法:
var data=db.orderInfo.Join(db.orderType,t=>t.orderTypeId,s=>s.id,(t,s)=>t).OrderByDescending(t=>t.orderId).
Select(
t=> new{
orderId=t.t.id,
orderTypeName=t.s.name,
...
}).ToList(); //使用Join关键字进行表连接
sql语法:(sql语句表关联有多种方法,在此只举一例)
string sqlStr=" SELECT * FROM orderInfo o ,orderType t WHERE o.orderTypeId=t.id ORDER BY t.createDate ";//使用Join关键字进行表连接组查询
(8) 关于in查询的语句:
Linq语法:
var data= from a in db.orderInfo where (new int?[2213,43311,32422]).Contains(a.orderId) select a ; //使用join关键字进行表连接
Lamda语法:
var data=db.orderInfo.Where(t=>(new int?[2213,43311,32422]).Contains(t.orderId)).ToList(); //使用Join关键字进行表连接
sql语法:
string sqlStr=" SELECT * FROM orderInfo WHERE orderId IN (2213,43311,32422) ";//使用Join关键字进行表连接组查询
(9) 关于去重查询的语句:
Linq语法:
var data= from a in db.orderInfo group p by new {a.orderTypeId} into _group select _group.FirstOrDefault(); //使用group关键字进行表数据去重
var data= from a in db.orderInfo group p by new {a.orderTypeId,...} into _group select _group.FirstOrDefault(); //使用group关键字对多个字段进行表数据去重
Lamda语法:
单个去重:
var data=db.orderInfo.GroupBy(t=>t.orderTypeId).Select(r => r.First()).ToList(); //使用GroupBy关键字进行表数据去重
var data=db.orderInfo.DistinctBy(t=>t.orderTypeId).ToList(); //使用DistinctBy关键字进行表数据去重
多个字段去重:
var data=db.orderInfo.GroupBy(t=>new{t.orderTypeId,...}).Select(r => r.First()).ToList(); //使用GroupBy关键字对多个字段进行表数据去重
var data=db.orderInfo.DistinctBy(t=>new{t.orderTypeId,...}).ToList(); //使用DistinctBy关键字对多个字段进行表数据去重
sql语法:
string sqlStr=" SELECT DISTINCT orderTypeId FROM orderInfo ";//使用DISTINCT 关键字进行表数据去重
string sqlStr=" SELECT orderTypeId FROM orderInfo GROUP BY orderTypeId ";//使用GROUP BY关键字进行表数据去重
https://blog.csdn.net/qq_42773229/article/details/88529288