展开/展平实体框架结果

发布于 2024-12-04 11:45:27 字数 2460 浏览 0 评论 0原文

原始 SQL 查询:

SELECT e.id, e.[type_id], e.name
FROM [user] u
JOIN user_group ug ON ug.[user_id] = u.id
JOIN usergroup grp on grp.id = ug.group_id
JOIN access_entity ae ON ae.group_id = grp.id
JOIN entity e on e.id = ae.entity_id
WHERE u.id = 184
GROUP BY e.id, e.[type_id], e.name

UNION

SELECT e.id, e.[type_id], e.name
FROM [user] u
JOIN user_group ug ON ug.[user_id] = u.id
JOIN usergroup grp on grp.id = ug.group_id
JOIN CRUD xs on xs.FK_Group_ID = grp.id
JOIN entity_type et on et.id = xs.FK_TypeID
JOIN entity e on e.[type_id] = et.id
WHERE u.id = 184 
AND e.confidential = 0 
AND xs.[Read] = 1
GROUP BY e.id, e.[type_id], e.name

翻译为 Linq to Sql:

var A = M.users
    .Join(M.user_groups, u => u.id, ug => ug.user_id, (u, ug) => new { u = u, ug = ug })
    .Join(M.usergroups, x => x.ug.group_id, grp => grp.id, (x, grp) => new { u = x.u, ug = x.ug, grp = grp })
    .Join(M.access_entities, x => x.grp.id, ae => ae.group_id, (x, ae) => new { u = x.u, ug = x.ug, grp = x.grp, ae = ae })
    .Join(M.entities, x => x.ae.entity_id, e => e.id, (x, e) => new { u = x.u, ug = x.ug, grp = x.grp, ae = x.ae, e = e })
    .Where(x => x.u.id == Global.CurrentUser.id);

var B = M.users
    .Join(M.user_groups, u => u.id, ug => ug.user_id, (u, ug) => new { u = u, ug = ug })
    .Join(M.usergroups, x => x.ug.group_id, grp => grp.id, (x, grp) => new { u = x.u, ug = x.ug, grp = grp })
    .Join(M.CRUDs, x => x.grp.id, xs => xs.FK_Group_ID, (x, xs) => new { u = x.u, ug = x.ug, grp = x.grp, xs = xs })
    .Join(M.entity_types, x => x.xs.FK_TypeID, et => et.id, (x, et) => new { u = x.u, ug = x.ug, grp = x.grp, xs = x.xs, et = et })
    .Join(M.entities, x => x.et.id, e => e.type_id, (x, e) => new { u = x.u, ug = x.ug, grp = x.grp, xs = x.xs, e = e })
    .Where(x => x.u.id == Global.CurrentUser.id && x.xs.Read && x.e.confidential == 0);

var RestrictedEntities = A.Select(x => x.e).Union(B.Select(x => x.e));

问题是实体框架不显示 user_group 等表,因为它只是一个 1:* 连接表。

在实体框架中,我基本上可以这样做:

IQueryable<IEnumerable<IEnumerable<entity>>> Entities = this.ObjectContext.users.Select(u => u.usergroups.Select(ug => ug.access_entity.Select(ae => ae.entity)));

有没有一种方法可以将其返回为

IQueryable<entity>

Original SQL Query:

SELECT e.id, e.[type_id], e.name
FROM [user] u
JOIN user_group ug ON ug.[user_id] = u.id
JOIN usergroup grp on grp.id = ug.group_id
JOIN access_entity ae ON ae.group_id = grp.id
JOIN entity e on e.id = ae.entity_id
WHERE u.id = 184
GROUP BY e.id, e.[type_id], e.name

UNION

SELECT e.id, e.[type_id], e.name
FROM [user] u
JOIN user_group ug ON ug.[user_id] = u.id
JOIN usergroup grp on grp.id = ug.group_id
JOIN CRUD xs on xs.FK_Group_ID = grp.id
JOIN entity_type et on et.id = xs.FK_TypeID
JOIN entity e on e.[type_id] = et.id
WHERE u.id = 184 
AND e.confidential = 0 
AND xs.[Read] = 1
GROUP BY e.id, e.[type_id], e.name

Translated to Linq to Sql:

var A = M.users
    .Join(M.user_groups, u => u.id, ug => ug.user_id, (u, ug) => new { u = u, ug = ug })
    .Join(M.usergroups, x => x.ug.group_id, grp => grp.id, (x, grp) => new { u = x.u, ug = x.ug, grp = grp })
    .Join(M.access_entities, x => x.grp.id, ae => ae.group_id, (x, ae) => new { u = x.u, ug = x.ug, grp = x.grp, ae = ae })
    .Join(M.entities, x => x.ae.entity_id, e => e.id, (x, e) => new { u = x.u, ug = x.ug, grp = x.grp, ae = x.ae, e = e })
    .Where(x => x.u.id == Global.CurrentUser.id);

var B = M.users
    .Join(M.user_groups, u => u.id, ug => ug.user_id, (u, ug) => new { u = u, ug = ug })
    .Join(M.usergroups, x => x.ug.group_id, grp => grp.id, (x, grp) => new { u = x.u, ug = x.ug, grp = grp })
    .Join(M.CRUDs, x => x.grp.id, xs => xs.FK_Group_ID, (x, xs) => new { u = x.u, ug = x.ug, grp = x.grp, xs = xs })
    .Join(M.entity_types, x => x.xs.FK_TypeID, et => et.id, (x, et) => new { u = x.u, ug = x.ug, grp = x.grp, xs = x.xs, et = et })
    .Join(M.entities, x => x.et.id, e => e.type_id, (x, e) => new { u = x.u, ug = x.ug, grp = x.grp, xs = x.xs, e = e })
    .Where(x => x.u.id == Global.CurrentUser.id && x.xs.Read && x.e.confidential == 0);

var RestrictedEntities = A.Select(x => x.e).Union(B.Select(x => x.e));

The problem is that the Entity Framework doesn't show tables like user_group, etc, since it's just a 1:* connection table.

In Entity Framework, I can basically do:

IQueryable<IEnumerable<IEnumerable<entity>>> Entities = this.ObjectContext.users.Select(u => u.usergroups.Select(ug => ug.access_entity.Select(ae => ae.entity)));

Is there a way to have that returned as just a

IQueryable<entity>

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2024-12-11 11:45:27

SelectMany 是您所追求的吗?

我相信

this.ObjectContext.users.SelectMany(
  u => u.usergroups.SelectMany(
    ug => ug.access_entity.Select(ae => ae.entity)));

应该有类型 IEnumerable

Is SelectMany what you are after?

I believe

this.ObjectContext.users.SelectMany(
  u => u.usergroups.SelectMany(
    ug => ug.access_entity.Select(ae => ae.entity)));

should have type IEnumerable<entity>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文