C#在EF框架中Linq和Lamda表达式
简单的查询语句:
Linq语法:
var data=from a in db.Areas select a ;
Lamda语法:
var data=db.Areas;
sql语法:
string sqlStr=" SELECT * FROM Areas ";
简单的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 ";
关于“COUNT、SUM、MIN、MAX”函数的语句:
Linq语法:
//查询该表中最大编号Id
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 ) ;
//查询该表数据总条数
var data=( from a in db.orderInfo select a ).Count() ;
//查询该表中所有消费额的总数(求和)
var data=( from a in db.orderInfo select a ).Sum( p=>p.orderMoney ) ;
Lamda语法:
//查询该表中最大编号Id
var data=db.orderInfo.Max( t=>t.orderId );
//查询该表中最小编号Id
var data=db.orderInfo.Min( t=>t.orderId );
//查询该表数据总条数
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 ";
关于数据排序的语句:
Linq语法:
//倒序排序,升序可用ascending关键字
var data=from a in db.orderInfo where a.orderId > 20 orderby a.orderId descending select a ;
Lamda语法:
情况一,根据单字段排序:
//倒序排序,升序可用OrderBy关键字
var data=db.orderInfo.OrderByDescending( t=>t.orderId ).Where( t=>t.orderId > 20 ) .ToList();
情况二,根据多字段主次排序:
//先根据年份升序排序,再根据月份升序排序
var priceMonthEntities = priceMonthApp.GetList().OrderBy(t => t.F_Year).ThenBy(t => t.F_Month).ToList();
sql语法:
//倒序排序,升序可用ASC关键字
string sqlStr=" SELECT * FROM orderInfo WHERE orderId > 20 ORDER BY orderId DESC ";
关于分页查询的语句:
Linq语法:
//pageIndex:当前页码,pageSize:分页数据显示条数
var data=( from a in db.orderInfo select a ) .Skip((pageIndex-1) * pageSize).Take(pageSize).ToList();
Lamda语法:
//pageIndex:当前页码,pageSize:分页数据显示条数
var data=db.orderInfo.Skip((pageIndex-1)* pageSize).Take(pageSize).ToList();;
sql语法:
//pageIndex:当前页码,pageSize:分页数据显示条数
string sqlStr=" SELECT TOP pageSize * FROM orderInfo WHERE orderId NOT IN ( SELECT TOP ( ( pageIndex - 1 ) * pageSize ) orderId FROM orderInfo ) ";
关于模糊查询(like)的语句:
Linq语法:
//使用Contains关键字进行模糊匹配
var data= from a in db.orderInfo where a.orderId.Contains(1) select a;
Lamda语法:
//使用Contains关键字进行模糊匹配
var data=db.orderInfo.Where(t=>t.F_UserId.Contains("1")).ToList();
sql语法:
//使用like关键字进行模糊匹配
string sqlStr=" SELECT * FROM orderInfo WHERE orderId LIKE '%12%' ";
关于分组查询的语句:
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语法:
//使用GroupBy关键字进行分组查询(单个字段)
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();
sql语法:
//使用 GROUP BY关键字进行分组查询
string sqlStr=" SELECT orderType ,SUM(orderMoney),MAX(orderMoney),MIN(orderMoney) FROM orderInfo GROUP BY orderType ";
关于多表关联查询的语句:
Linq语法:
//使用join关键字进行表连接
var data= from a in db.orderInfo join e in db.orderType on a.orderTypeId equals e.id select r ;
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关键字进行表连接
EF Core中的写法:
var data=db.orderInfo.Join(db.orderType,t=>t.orderTypeId,s=>s.id,(t,s)=>new{
orderId=s.Id,
.....
}).toList();
sql语法:(sql语句表关联有多种方法,在此只举一例)
//使用Join关键字进行表连接组查询
string sqlStr=" SELECT * FROM orderInfo o ,orderType t WHERE o.orderTypeId=t.id ORDER BY t.createDate ";
关于in查询的语句:
Linq语法:
//使用join关键字进行表连接
var data= from a in db.orderInfo where (new int?[2213,43311,32422]).Contains(a.orderId) select a ;
Lamda语法:
//使用Join关键字进行表连接
var data=db.orderInfo.Where(t=>(new int?[2213,43311,32422]).Contains(t.orderId)).ToList();
sql语法:
//使用Join关键字进行表连接组查询
string sqlStr=" SELECT * FROM orderInfo WHERE orderId IN (2213,43311,32422) ";
关于去重查询的语句:
Linq语法:
//使用group关键字进行表数据去重
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();
Lamda语法:
单个去重:
//使用GroupBy关键字进行表数据去重
var data=db.orderInfo.GroupBy(t=>t.orderTypeId).Select(r => r.First()).ToList();
//使用DistinctBy关键字进行表数据去重
var data=db.orderInfo.DistinctBy(t=>t.orderTypeId).ToList();
多个字段去重:
//使用GroupBy关键字对多个字段进行表数据去重
var data=db.orderInfo.GroupBy(t=>new{t.orderTypeId,...}).Select(r => r.First()).ToList();
//使用DistinctBy关键字对多个字段进行表数据去重
var data=db.orderInfo.DistinctBy(t=>new{t.orderTypeId,...}).ToList();
sql语法:
//使用DISTINCT 关键字进行表数据去重
string sqlStr=" SELECT DISTINCT orderTypeId FROM orderInfo ";
//使用GROUP BY关键字进行表数据去重
string sqlStr=" SELECT orderTypeId FROM orderInfo GROUP BY orderTypeId ";
版权声明:
作者:亦灵一梦
链接:https://blog.haokaikai.cn/2020/program/aspnet/1034.html
来源:开心博客
文章版权归作者所有,未经允许请勿转载。
THE END
1
二维码
海报
C#在EF框架中Linq和Lamda表达式
简单的查询语句:
Linq语法:
var data=from a in db.Areas select a ;
Lamda语法:
var data=db.Areas;
sql语法:
string sqlStr=" SELECT * FROM Area……