Uploaded by Marvin Lai

Entity Framework常用查询,EF join,EF多表联查 cxnwb的博客-CSDN博客 entityframework多表查询

advertisement
博客
下载
学习
社区
GitCode
云服务
猿如意
搜索
数据库开发
Entity Framework常用查询,EF join,EF多表联查
于 2021-09-12 22:19:58 发布
cxnwb
1723
收藏 11
版权
文章标签: 数据库开发
Ef 两表Join
linq写法
1
//两表join linq写法
2
var query = from u in oae.Users
3
join p in oae.Parent on u.Id equals p.ParentId
4
select new
5
{
6
username = u.UserName,
7
father = p.Father
8
};
lamdba写法:
1
/*
2
第一个参数:
3
第二,三参数: 连接条件
4
5
第四个参数:
join的表
返回值
*/
6
var query = oae.Users.Join(oae.Parent, a => a.Id, b => b.ParentId, (a, b) => new
7
{ 8
9
10
username = a.UserName,
fahter = b.Father
});
Ef 两表 left Join
linq写法:
1
//两表left join linq写法
2
var query = from u in oae.Users
3
join p in oae.Parent on u.Id equals p.ParentId into jtemp
4
from leftjoin in jtemp.DefaultIfEmpty()
5
select new
6
{
7
username = u.UserName,
8
father = leftjoin.Father
9
};
lamdba写法:
1
//两表left join lamdba写法
2
var query = oae.Users.GroupJoin(oae.Parent, a => a.Id, b => b.ParentId, (a, b) => new
3
{ 4
5
username = a.UserName,
parent = b
6
}).SelectMany(a => a.parent, (m, n) => new
7
{
8
username = m.username,
9
10
father = n.Father
});
lamdba的写法主要用到了groupjoin与SelectMany,这里简单解释一下:
groupjoin:
用于查询一对多的关系很方便,所以得数据格式就是1对多的关系
cxnwb
关注
登录/注册
会员中心
足迹
动
SelectMany:
可以解析集合中含有集合的情况(也就是1对多的表现)为单一对象
Ef三表Join
linq写法:
1
//三表join linq写法
2
var queru = from u in oae.Users
3
join p in oae.Parent on u.Id equals p.ParentId
4
join s in oae.Score on u.Id equals s.UsersId
5
select new
6
{
7
username = u.UserName,
8
fahter = p.Father,
9
sub = s.Sub,
10
score = s.Score1
11
};
lamdba写法:
1
//三表join lamdba写法
2
var query = oae.Users.Join(oae.Parent, a => a.Id, b => b.ParentId, (a, b) => new
3
{
4
uid = a.Id,
5
username = a.UserName,
6
father = b.Father
7
}).Join(oae.Score, a => a.uid, b => b.UsersId, (m, n) => new
8
{
9
username = m.username,
10
father = m.father,
11
sub = n.Sub,
12
score = n.Score1
13
});
其实和两表join类似,往后面点就行了
Ef三表left Join
Linq写法:
1
//三表left join linq写法
2
var query = from u in oae.Users
3
join p in oae.Parent on u.Id equals p.ParentId into ptemp
4
join s in oae.Score on u.Id equals s.UsersId into stemp
5
from leftp in ptemp.DefaultIfEmpty()
6
from lefts in stemp.DefaultIfEmpty()
7
select new
8
{
9
username = u.UserName,
10
father = leftp.Father,
11
sub = lefts.Sub,
12
score = lefts.Score1
13
};
lamdba写法:
1
//三表left join lamdba写法
2
var query = oae.Users.GroupJoin(oae.Parent, a => a.Id, b => b.ParentId, (a, b) => new
3
{ 4
5
uid = a.Id,
username = a.UserName,
6
parent = b
7
}).GroupJoin(oae.Score, a => a.uid, b => b.UsersId, (m, n) => new
8
{
9
10
username = m.username,
uid = m.uid,
cxnwb
关注
11
score = n,
12
parent = m.parent
13
}).SelectMany(a => a.parent.DefaultIfEmpty(), (m, n) => new
14
{
15
username = m.username,
16
fahter = n.Father,
17
score = m.score
18
}).SelectMany(a => a.score.DefaultIfEmpty(), (m, n) => new
19
{
20
usernaem = m.username,
21
father = m.fahter,
22
sub = n.Sub,
23
score = n.Score1
24
});
lamdba写法2:上面是现join完在selectmany,也可以先selectmany了在join第三张表
1
//三表left join lamdba写法2
2
var query = oae.Users.GroupJoin(oae.Parent, a => a.Id, b => b.ParentId, (a, b) => new
3
{ 4
5
uid = a.Id,
username = a.UserName,
6
parent = b
7
}).SelectMany(a => a.parent.DefaultIfEmpty(), (m, n) => new
8
{
9
uid = m.uid,
10
username = m.username,
11
father = n.Father
12
}).GroupJoin(oae.Score, a => a.uid, b => b.UsersId, (m, n) => new
13
{
14
username = m.username,
15
father = m.father,
16
score = n
17
}).SelectMany(a => a.score, (m, n) => new
18
{
19
username = m.username,
20
father = m.father,
21
sub = n.Sub,
22
score = n.Score1
23
});
单表分组函数
linq:
1
//linq
2
var query = from score in oae.Score
3
group score by score.Sub into grouptemp
4
select new
5
{
6
sub = grouptemp.Key,
7
sum = grouptemp.Sum(a => a.Score1),
8
max = grouptemp.Max(a => a.Score1),
9
min = grouptemp.Min(a => a.Score1),
10
avg = grouptemp.Average(a => a.Score1)
11
};
lamdba:
1
//lamdba
2
var query = oae.Score.GroupBy(a => a.Sub).Select(grouptemp => new
3
{
4
sub = grouptemp.Key,
5
sum = grouptemp.Sum(a => a.Score1),
6
max = grouptemp.Max(a => a.Score1),
7
min = grouptemp.Min(a => a.Score1),
8
9
avg = grouptemp.Average(a => a.Score1)
}).Where(a => a.max > 60);
cxnwb
关注
10
11
var result = query.ToList();
分组函数后接一点条件
linq:
1
//linq
2
var query = from score in oae.Score
3
group score by score.Sub into grouptemp
4
where grouptemp.Sum(a=>a.Score1)>60
5
select new
6
{
7
sub = grouptemp.Key,
8
sum = grouptemp.Sum(a => a.Score1),
9
max = grouptemp.Max(a => a.Score1),
10
min = grouptemp.Min(a => a.Score1),
11
avg = grouptemp.Average(a => a.Score1)
12
};
linq写法2:
1
//linq
2
var query = from score in oae.Score
3
group score by score.Sub into grouptemp
4
select new
5
{
6
sub = grouptemp.Key,
7
sum = grouptemp.Sum(a => a.Score1),
8
max = grouptemp.Max(a => a.Score1),
9
min = grouptemp.Min(a => a.Score1),
10
avg = grouptemp.Average(a => a.Score1)
11
} into temp
12
where temp.max > 60
13
select new
14
{
15
sub = temp.sub,
16
sum = temp.sum
17
18
};
var result = query.ToList();
文章知识点与官方知识档案匹配,可进一步学习相关知识
MySQL入门技能树 连接查询
INNER JOIN
20555 人正在系统学习中
EntityFramework数据查询
XIAO_11_DONG的博客
147
Entity Framework Core 数据库简单设计,数据查询、修改、保存
entity framework 多表查询方式
12-27
文件全面的提供了多表查询的方式,相信会帮倒大家的。
...查询_m0_58645044的博客_entityframework多表查询
10-20
EntityFramework多表增删改查一.数据库、数据表的创建1.创建Catelog表在Catelog中添加数据2.创建Article表…
Entity framework多表查询_连环炮的博客
10-10
public IQueryable<BasicComponentView> DaoChuData(YKTEntities db, string order, string sort, QueryEntity …
EF 多表查询的方法
findsafety的专栏
3万+
EF实体类 entity = new EF实体类(); var query = (from u in entityt.用户表 join b in entity.权限表 on u.用户权限Id…
EF实现多表连接查询
OAOE02的博客
2310
建立数据库(BOOK) 建立数据表(Articles,Category) 表 Articles 字段名 类型 约束 ID int 主键 自增 非空 Title nvar…
EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right...
cxnwb
10-10
关注
场景:在实际的项目中使用EntityFramework都会遇到使用Ef处理连接查询的问题,这里做一些小例子如何通过Lin…
Download