排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术
原
Entity Framework常用查询,EF join,EF多表联查,原生sql。EF 多表查询。AsNoTracking
分类:
EF
直接执行sql语句
//全表查询 List<Users> ulist = se.Database.SqlQuery<Users>("select * from users").ToList();
接条件很也方便
//接条件查询 List<Users> ulist = se.Database.SqlQuery<Users>("select * from users where number=@number", new SqlParameter("number", "NS001")).ToList();
执行添加,删除,修改等操作
int count =Database.ExecuteSqlRaw(sql,params);
执行添加,删除,修改等操作 方法2:
context.Database.ExecuteSqlInterpolated()
直接执行存储过程语句
//执行存储过程并取得返回值 int prlr = myc.Database.SqlQuery<int>("exec [ProSelectCount] '1'").SingleOrDefault();
AsNoTracking()不跟踪上下文可以提高查询效率
context.Article.AsNoTracking()
Ef 两表Join
linq写法:
//两表join linq写法 var query = from u in oae.Users join p in oae.Parent on u.Id equals p.ParentId select new { username = u.UserName, father = p.Father };
lamdba写法:
/* 第一个参数: join的表 第二,三参数: 连接条件 第四个参数: 返回值 */ var query = oae.Users.Join(oae.Parent, a => a.Id, b => b.ParentId, (a, b) => new { username = a.UserName, fahter = b.Father });
Ef 两表 left Join
linq写法:
//两表left join linq写法 var query = from u in oae.Users join p in oae.Parent on u.Id equals p.ParentId into jtemp from leftjoin in jtemp.DefaultIfEmpty() select new { username = u.UserName, father = leftjoin.Father };
lamdba写法:
//两表left join lamdba写法 var query = oae.Users.GroupJoin(oae.Parent, a => a.Id, b => b.ParentId, (a, b) => new { username = a.UserName, parent = b }).SelectMany(a => a.parent, (m, n) => new { username = m.username, father = n.Father });
lamdba的写法主要用到了groupjoin与SelectMany,这里简单解释一下:
groupjoin: 用于查询一对多的关系很方便,所以得数据格式就是1对多的关系
SelectMany: 可以解析集合中含有集合的情况(也就是1对多的表现)为单一对象
Ef三表Join
linq写法:
//三表join linq写法 var queru = from u in oae.Users join p in oae.Parent on u.Id equals p.ParentId join s in oae.Score on u.Id equals s.UsersId select new { username = u.UserName, fahter = p.Father, sub = s.Sub, score = s.Score1 };
lamdba写法:
//三表join lamdba写法 var query = oae.Users.Join(oae.Parent, a => a.Id, b => b.ParentId, (a, b) => new { uid = a.Id, username = a.UserName, father = b.Father }).Join(oae.Score, a => a.uid, b => b.UsersId, (m, n) => new { username = m.username, father = m.father, sub = n.Sub, score = n.Score1 });
其实和两表join类似,往后面点就行了
Ef三表left Join
Linq写法:
//三表left join linq写法 var query = from u in oae.Users join p in oae.Parent on u.Id equals p.ParentId into ptemp join s in oae.Score on u.Id equals s.UsersId into stemp from leftp in ptemp.DefaultIfEmpty() from lefts in stemp.DefaultIfEmpty() select new { username = u.UserName, father = leftp.Father, sub = lefts.Sub, score = lefts.Score1 };
lamdba写法:
//三表left join lamdba写法 var query = oae.Users.GroupJoin(oae.Parent, a => a.Id, b => b.ParentId, (a, b) => new { uid = a.Id, username = a.UserName, parent = b }).GroupJoin(oae.Score, a => a.uid, b => b.UsersId, (m, n) => new { username = m.username, uid = m.uid, score = n, parent = m.parent }).SelectMany(a => a.parent.DefaultIfEmpty(), (m, n) => new { username = m.username, fahter = n.Father, score = m.score }).SelectMany(a => a.score.DefaultIfEmpty(), (m, n) => new { usernaem = m.username, father = m.fahter, sub = n.Sub, score = n.Score1 });
lamdba写法2:上面是现join完在selectmany,也可以先selectmany了在join第三张表
//三表left join lamdba写法2 var query = oae.Users.GroupJoin(oae.Parent, a => a.Id, b => b.ParentId, (a, b) => new { uid = a.Id, username = a.UserName, parent = b }).SelectMany(a => a.parent.DefaultIfEmpty(), (m, n) => new { uid = m.uid, username = m.username, father = n.Father }).GroupJoin(oae.Score, a => a.uid, b => b.UsersId, (m, n) => new { username = m.username, father = m.father, score = n }).SelectMany(a => a.score, (m, n) => new { username = m.username, father = m.father, sub = n.Sub, score = n.Score1 });
单表分组函数
linq:
//linq var query = from score in oae.Score group score by score.Sub into grouptemp select new { sub = grouptemp.Key, sum = grouptemp.Sum(a => a.Score1), max = grouptemp.Max(a => a.Score1), min = grouptemp.Min(a => a.Score1), avg = grouptemp.Average(a => a.Score1) };
lamdba:
//lamdba var query = oae.Score.GroupBy(a => a.Sub).Select(grouptemp => new { sub = grouptemp.Key, sum = grouptemp.Sum(a => a.Score1), max = grouptemp.Max(a => a.Score1), min = grouptemp.Min(a => a.Score1), avg = grouptemp.Average(a => a.Score1) }).Where(a => a.max > 60); var result = query.ToList();
分组函数后接一点条件
linq:
//linq var query = from score in oae.Score group score by score.Sub into grouptemp where grouptemp.Sum(a=>a.Score1)>60 select new { sub = grouptemp.Key, sum = grouptemp.Sum(a => a.Score1), max = grouptemp.Max(a => a.Score1), min = grouptemp.Min(a => a.Score1), avg = grouptemp.Average(a => a.Score1) };
linq写法2:
//linq var query = from score in oae.Score group score by score.Sub into grouptemp select new { sub = grouptemp.Key, sum = grouptemp.Sum(a => a.Score1), max = grouptemp.Max(a => a.Score1), min = grouptemp.Min(a => a.Score1), avg = grouptemp.Average(a => a.Score1) } into temp where temp.max > 60 select new { sub = temp.sub, sum = temp.sum }; var result = query.ToList();
两表分组函数
对某个考生的成绩统计
linq形式:
var query = from u in oae.Users join s in oae.Score on u.Id equals s.UsersId select new { UserName = u.UserName, Score1 = s.Score1 } into jointemp group jointemp by jointemp.UserName into a select new ScoreViewModel { UserName = a.Key, Count = a.Count(), Max = a.Max(b => b.Score1), Min = a.Min(b => b.Score1), Sum = a.Sum(b => b.Score1), Avg = a.Average(b => b.Score1) }; List<ScoreViewModel> result = query.ToList();
三表分组函数
对某个考生的成绩统计,并包含考生父母
linq形式:
var query = from u in oae.Users join p in oae.User_Parent on u.Id equals p.UsersId into upjointemp from leftjoin in upjointemp.DefaultIfEmpty() join s in oae.Score on u.Id equals s.UsersId select new { UserName = u.UserName, Father = leftjoin.Father, Score1 = s.Score1 } into jointemp group jointemp by new { jointemp.UserName, jointemp.Father } into a select new ScoreViewModel { UserName = a.Key.UserName, Father = a.Key.Father ?? "孤儿", Count = a.Count(), Max = a.Max(b => b.Score1), Min = a.Min(b => b.Score1), Sum = a.Sum(b => b.Score1), Avg = a.Average(b => b.Score1) }; List<ScoreViewModel> result = query.ToList();
未完待续........
欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739
评价