简体   繁体   English

实体框架复杂的多个 where 条件

[英]Entity Framework complex multiple where condition

I have a database in which I'm trying to track document approvals.我有一个数据库,我试图在其中跟踪文档审批。 Each document has 1 or more tasks.每个文档有 1 个或多个任务。 Each task has 1 or more approvals.每个任务都有 1 个或多个批准。 Each approval has the ID to the assigned user as well as an assigned group.每个批准都有分配给分配的用户和分配的组的 ID。 Each user may belong to 1 or more groups and may have various permissions in each group.每个用户可能属于 1 个或多个组,并且在每个组中可能拥有不同的权限。 This information is stored in a 'many to many' relationship table with an additional field for permissions level.此信息存储在“多对多”关系表中,并具有用于权限级别的附加字段。 (user_id, group_id, permission_id) (user_id, group_id,permission_id)

For example, an approval to a task may be assigned to a user with ID=5 and to a group with ID=7.例如,对任务的批准可以分配给 ID=5 的用户和 ID=7 的组。 In order for a user to approve this approval, they either must have a user ID of 5 or belong to group 7 with the permission to approve.为了让用户批准此批准,他们的用户 ID 必须为 5 或属于具有批准权限的组 7。

I'd like to create a query which selects all approvals either assigned to a particular user or belong to a group that a particular user have 'awareness' permissions to.我想创建一个查询,该查询选择分配给特定用户或属于特定用户具有“意识”权限的组的所有批准。

I have easily been able to query for just the user ID with:我已经能够轻松地查询用户 ID:

var approvals = context.APPROVALS
                       .Where(a => a.APPROVER_ID == CurrentUserID)
                       .ToList();

My issue is also checking against the groups that the current user has 'awareness' permissions to.我的问题还在于检查当前用户具有“意识”权限的组。 The best I have been able to come up with is storing a list of the users 'awareness' groups (stored as CurrentUserAwarnessGroups in the example below) and doing something like the following:我能想到的最好的方法是存储用户“意识”组的列表(在下面的示例中存储为 CurrentUserAwarnessGroups)并执行如下操作:

var approvals = context.APPROVALS
                       .Where(a => a.APPROVER_ID == CurrentUserID || CurrentUserAwarnessGroups.Contains(a.GROUP_ID))

This query method fails for me not to mention I am assuming it must perform at least part of this query on the client side which means a much larger (and lengthier) query is returned.这个查询方法对我来说失败了,更不用说我假设它必须至少在客户端执行这个查询的一部分,这意味着返回一个更大(更长)的查询。

Can anyone suggest a better way to perform this functionality?任何人都可以提出更好的方法来执行此功能吗? I can change my database if necessary.如有必要,我可以更改我的数据库。

So I was able to figure out a better method.所以我能够想出一个更好的方法。 My approach was incorrect.我的做法是错误的。 Rather than trying to query approvals and include the related user, I queried by user and included related approvals purely based on their group.我没有尝试查询批准并包含相关用户,而是按用户查询并纯粹基于他们的组包含相关批准。 I determined the approval group will always contain the approver ID therefore I can query the database by group alone and then filter by approver on the client machine.我确定批准组将始终包含批准者 ID,因此我可以单独按组查询数据库,然后在客户端计算机上按批准者进行过滤。 Additionally, by using bitwise comparisons to control 'membership' as recommended by mxmissile, I am able to simplify my group-to-user table.此外,通过使用按位比较来控制 mxmissile 推荐的“成员资格”,我能够简化我的组到用户表。

My solution looks like:我的解决方案如下:

var user = context.USERs
                  .Where(u => u.USER_ID == CurrentUserID)
                  .FirstOrDefault();

var tempUserApprovals = user.GROUPTOUSER.SelectMany("GROUP.APPROVALS");
var userApprovals = new ObservableCollection<APPROVAL>();

foreach(var approval in tempUserApprovals)
{
    foreach(var group in CurrentUsersGroups)
    {
        if(approval.GROUP.GROUP_ID == group.GROUP_ID && ((int)group.MEMBERSHIP & Membership.is_aware == Membership.is_aware || approval.APPROVER_ID == CurrentUserID))
        {
            userApprovals.Add(approval);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM