(1) 基础的查询语句
Linq语法: var data = from a in Areas select a ;
Lamda语法: var data = Areas;
sql语法: select * from Areas ;
(2) 条件查询语句
Linq语法: var data = from a in Areas where a.id<21 select a;
Lamda语法: var data = Areas.Where( t=>t.orderId < 21) ;
sql语法: select * from Areas where id < 21 ;
(3) 关于“COUNT、SUM、MIN、MAX”函数的语句
Linq语法:
var maxOrder = ( from a in orderInfo select a ).Max( p=>p.orderId ) ; //查询表中最大编号Id
var minOrder = ( from a in orderInfo select a ).Min( p=>p.orderId ) ; //查询表中最小编号Id
var orderCount = ( from a in orderInfo select a ).Count ; //查询表数据总条数
var sellTotal = ( from a in orderInfo select a ).Sum( p=>p.sellNum ) ; //查询表中所有收入额的总数(求和)
Lamda语法:
var maxOrder = orderInfo.Max( t=>t.orderId ); //查询该表中最大编号Id
var minOrder = orderInfo.Min( t=>t.orderId ); //查询该表中最小编号Id
var orderCount = orderInfo.Count; //查询该表数据总条数
var sellTotal = orderInfo.Sum( t=>t.sellNum ); //查询该表中所有收入额的总数(求和)
sql语法:
select MAX(orderId) from orderInfo;//最大编号Id
select MIN(orderId) from orderInfo;//最小编号Id
select COUNT(*) from orderInfo;//数据总条数
select SUM(sellNum) from orderInfo;//所有sellNum(收入额)的总数(求和)
(4) 关于数据排序的语句
Linq语法:
var data = from a in db.orderInfo where a.orderId > 20 orderby a.orderId descending select a ; //倒序排序descending,升序可用ascending关键字
Lamda语法:
//情况一,根据单字段排序:
var data = orderInfo.OrderByDescending( t=>t.orderId ).Where( t=>t.orderId > 20) .ToList; //倒序排序OrderByDescending,升序可用OrderBy关键字
//情况二,根据多字段主次排序:
var priceMonthEntities = priceMonthApp.GetList.OrderBy(t => t.F_Year).ThenBy(t => t.F_Month).ToList; //先按年升序,再按月升序
sql语法:
select * from orderInfo where orderId > 20 ORDER BY orderId DESC; //倒序排序DESC,升序可用ASC关键字
(5) 关于分页查询的语句
Linq语法:
var data = ( from a in orderInfo select a ) .Skip((pageIndex -1) * pageSize).Take(pageSize).ToList;
Lamda语法:
var data = orderInfo.Skip((pageIndex -1)* pageSize).Take(pageSize).ToList; //pageIndex:当前页码,pageSize:分页数据显示条数
sql语法:
select TOP pageSize * from orderInfo where orderId NOT IN(select TOP( ( pageIndex - 1) * pageSize) orderId from orderInfo);
(6) 关于模糊查询LIKE的语句
Linq语法:
var data = from a in orderInfo where a.orderId.Contains(12) select a; //使用Contains关键字进行模糊匹配
Lamda语法:
var data = orderInfo.Where(t => t.orderId.Contains(12)).ToList; //使用Contains关键字进行模糊匹配
sql语法:
select * from orderInfo where orderId LIKE '%12%'"; //使用like关键字进行模糊匹配
(7) 关于分组查询的语句
Linq语法:
var data = from a in orderInfo orderby a.orderId descending
group a by a.orderType into s select new { s.key, //分组字段
s.sumMoney = s.Sum(a=>a.orderMoney), //分组后算出总的消费额
s.maxMoney = s.Max(a=>a.orderMoney), //分组后算出最大的消费额
s.minMoney = s.Min(a=>a.orderMoney) //分组后算出最小的消费额
};
Lamda语法:
//使用GroupBy关键字进行分组查询(单个字段)
var data = 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语法:
select orderType , SUM(orderMoney), MAX(orderMoney), MIN(orderMoney) from orderInfo GROUP BY orderType;
(8) 关于in查询的语句
Linq语法:
var data = from a in invAll where (new int[] { 2213, 43311,32455 }).Contains(a.orderId) select a;
Lamda语法:
var data=db.orderInfo.Where(t=>(new int[] { 2213, 43311,32455 }).Contains(t.orderId)).ToList;
sql语法:
select * from orderInfo where orderId IN (2213, 43311,32455);