https://www.cnblogs.com/xszjk/articles/5517588.html
本文纯手写,可能单词有误
1.普通
var s=from a in table select a;
var s1=table;
2.接where条件
var s=from a in table where a.id>10 select a;
var s1=table.where(a=>a.id>10);
3.简单函数计算
//获取最大的Id
var s=(from a in table select a).max(p=>p.id);
//获取最小值
var s1=(from a in table select a).min(p=>p.id);
//获取结果的总数量
var s2=(from a in table select a).count();
//获取年龄的和
var s3=(from a in table select a).sum(p=>p.ages);
//获取最大的id
var m=table.max(a=>a.id);
//获取最小值
var m1=table.min(a=>a.id);
//获取结果总数
var m2=table.count();
//获取年龄的和
var m3=table.sum(a=>a.ages);
4.排序order by desc/asc
var s=from a in table where s.id >10
orderby a.id descending //倒序
// orderby a.id ascending //正序
select a;
var m=table.orderby(a=>a.id).where(a=>a.id>10); //正序
var m2=table.orderbydescending(a=>a.id>10);//倒序
5.top(1)取第一条数据
var s=(from a in table select a).FirstOrDefault();
var m=table.FirstOrDefault();
5.1取最后一条数据
var s=(from a in table orderby a.id descending select a).FirstOrDefault();
var m=table.orderbydescending(a=>a.id).FirstOrDefault();
6.跳过前面多少条数据取余下的数据
var s=(from a in table orderby a.id descending).skip(10);
var m=table.orderbydescending(a=>a.id).skip(10).Tolist();
7.分页数据查询
//取第11条到第20条数据
var s=(from a in table where a.id>10 orderby a.id descending select a).skip(10).Take(10);
var m=table.orderbydescending(a=>a.id).where(a=>a.id>10).skip(10).Take(10);
8.包含,类似like '%%' where u.EndsWith("Y")
//查询名称含有“张”的人
var s=from a in table
where a.Names.Contains("张")
select a;
var m=table.where(a=>a.Names.Contains("张")).Tolist;
strng sql="select * from htable where Name like'%张%'";
9.分组group by
var s=from a in table ordeby a.id desceding
group a by a.Names 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
}
foreach (var t in ss){
Response.Write(t.Key + "--" + t.rpId + "--" + t.MaxRpId + "--" + t.MinRpId);
}
var s2=from a in table orderby a.id descending
group a by a.Names into n
select n;
foreach (var t in ss1){
Response.Write(t.Key + "--" + t.Min(p => p.rpId));
}
var m=table.groupby(a=>a.Names);
foreach (var t in ss2){
Response.Write(t.Key + "--" + t.Min(p => p.rpId));
}
10.连接查询
var s=from a in table
join b in table2 on a.id equals b.id
orderby a.id descending
select a;
var m=table.join(table2, a=>a.id,b=>b.id,(a,b)=>a).orderbyDescending(a=>a.id).Tolist();
11.sql中的In
var s=from p in table
where (new int?[] {24,25,26}).Contains(p.id)
select p;
//2
string sql = "select * from Am_recProScheme where rpId in(24,25,26)";
LINQ标准查询操作符(二)——Join、GroupJoin、GroupBy、Concat、
四、联接操作符
https://www.cnblogs.com/dotnetmvc/p/3680151.html
join查询
//查询语法
var query =
(from p in db.Products
join c in db.Categories on p.CategoryID equals c.CategoryID
where p.CategoryID == 1
select new { p.ProductID, p.ProductName, c.CategoryID, c.CategoryName }).ToList();
//方法语法
var m=table.join(table,a=>a.id,b=>b.id,(a,b)=>{a.id,b.name,b.id}).where(a=>a.id==2);
// 分组链表查询GroupJoin
var m = (await _ClassRepository.GetAllListAsync()).GroupJoin((await _StudentRepository.GetAllListAsync()), a => a.Id, b => b.CId,
(a, b) => new ClassAndStudentGroupjoinDto
{
ClasssName = a.ClasssName,
Students = ObjectMapper.Map<List<Student>>(b),
}).ToList();
into:把前面查询的结果和结果结构注入到一个新自动生成的对象里面,例如
var s = from a in await _ClassRepository.GetAllListAsync()
join b in await _StudentRepository.GetAllListAsync() on a.Id equals b.CId into c
select new
{
a.ClasssName,
c
};
into注入到了c。
异步方法
一般是 await 具体语法.to...Async();要引入命名空间: using Microsoft.EntityFrameworkCore;
如:
var model2 = await _ApplydboRepository.GetAll().Join(_LogssRepository.GetAll(), a => a.Apptimes, b => b.Datatimes,
(a, b) => b).Where(b => b.Correct != null).FirstOrDefaultAsync();
var model3 = await _ApplydboRepository.GetAll().Join(_LogssRepository.GetAll(), a => a.Apptimes, b => b.Datatimes,
(a, b) => b).Where(b => b.Correct != null).ToListAsync();