LINQ语法
var data=from a in db.Areas select a ;
Lamda语法
var data=db.Areas;
SQL语法
string sqlStr=" SELECT * FROM Areas ";
LINQ语法
var data=from a in db.orderInfo where a.orderId > 20 select a ;
Lamda语法
var data=db.orderInfo.Where( t=>t.orderId > 20 ) ;var data=db.Areas;
SQL语法
string sqlStr=" SELECT * FROM orderInfo WHERE orderId > 20 ";
LINQ语法
var data=( from a in db.orderInfo select a ).Count() ;//查询该表数据总条数
Lamda语法
var data=db.orderInfo.Count();//查询该表数据总条数
SQL语法
string sqlStr=" SELECT COUNT(*) FROM orderInfo ";
LINQ语法
var data=( from a in db.orderInfo select a ).Max( p=>p.orderId ) ;//查询该表中最大编号Id
Lamda语法
var data=db.orderInfo.Max( t=>t.orderId );//查询该表中最大编号Id
SQL语法
string sqlStr=" SELECT MAX(orderId) FROM orderInfo ";
LINQ语法
var data=( from a in db.orderInfo select a ).Min( p=>p.orderId ) ;//查询该表中最小编号Id
Lamda语法
var data=db.orderInfo.Min( t=>t.orderId );//查询该表中最小编号Id
SQL语法
string sqlStr=" SELECT MIN(orderId) FROM orderInfo ";
LINQ语法
var data=( from a in db.orderInfo select a ).Sum( p=>p.orderMoney ) ;//查询该表中所有消费额的总数(求和)
Lamda语法
var data=db.orderInfo.Sum( t=>t.orderMoney );//查询该表中所有消费额的总数(求和)
SQL语法
string sqlStr=" SELECT SUM(orderMoney ) FROM orderInfo ";
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关键字
LINQ语法
var data=( from a in db.orderInfo select a ) .Skip((pageIndex-1) * pageSize).Take(pageSize).ToList();
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)";
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关键字进行模糊匹配
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语法
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语法
string sqlStr="SELECT * FROM orderInfo o ,orderType t WHERE o.orderTypeId=t.id ORDER BY t.createDate";
LINQ语法
var data= from a in db.orderInfo where (new int?[2213,43311,32422]).Contains(a.orderId) select a ;
Lamda语法
var data=db.orderInfo.Where(t=>(new int?[2213,43311,32422]).Contains(t.orderId)).ToList();
SQL语法
string sqlStr="SELECT * FROM orderInfo WHERE orderId IN (2213,43311,32422)";
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关键字进行表数据去重
LINQ语法
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;var data= from a in db.orderInfo where (new int?[2213,43311,32422]).Contains(a.orderId) select a ;
Lamda语法
var ss1 = db.Am_recProScheme.Join(db.Am_Test_Result, p => p.rpId, r => r.rsId, (p, r) => p).OrderByDescending(p => p.rpId).ToList();
SQL语法
string sssql = "SELECT r.* FROM Am_recProScheme AS r INNER JOIN Am_Test_Result AS t ON r.[rpId] = t.[rsId] ORDER BY r.[rpId] DESC";
转载至:https://www.cnblogs.com/lgx5/p/14899731.html