1
オリジナルSQLクエリ: アンワインド/フラット化Entity Frameworkの結果
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));
問題は、Entity Frameworkのは、など、USER_GROUPのようなテーブルが表示されないということです、それはちょうど1:*接続テーブルなので。 Entity Frameworkので
、私は基本的に行うことができます。
IQueryable<IEnumerable<IEnumerable<entity>>> Entities = this.ObjectContext.users.Select(u => u.usergroups.Select(ug => ug.access_entity.Select(ae => ae.entity)));
はそれがちょうど
IQueryable<entity>
として戻ってきた方法はありますか?
SelectMany(http://msdn.microsoft.com/en-us/library/bb534336.aspx)後で何ですか?私はthis.ObjectContext.users.SelectMany(u => u.usergroups.SelectMany(ug.access_entity.Select(ae => ae.entity)))はIEnumerable型がであると信じています。 –
Rune
ありがとう、それはそれだった:)あなたが答えとしてそれを投稿すれば、私はそれを受け入れるだろう。 – NKCSS
答えとして掲示される。ありがとう。 – Rune