【问题标题】:Linq query parent child grouping issueLinq查询父子分组问题
【发布时间】:2012-04-17 00:41:42
【问题描述】:

我有以下表格

Actions    
  ActionID   Action  NextActionID

    1        Submit      2
    2        Forward     3
    3        Approve    NULL

UserActionRights
UserID ActionID
  5     1
  6     2
  7     3
  8     2

我想要这样的输出

Action(Key) UserIDs(List)
  1          6
             7

  2          7
  3         "Empty List"

我尝试过的如下

(from a in actions join uar in UserActionRights on a.NextActionID equals uar.ActionID
Select new 
{
  Action=a.ActionID,
  UserIDs=uar.UserID

}).ToList().AsEnumerable().GroupBy(x => x.ActionID).Select(y => new 
                       {
                               Action = y.Key,
                               UserIDs = y.Select(tp=>tp.UserID)
}).ToList()

现在,当 ID=5 的用户登录时,我得到空输出,因为它没有 ActionID=2,3 的权限。我需要修改查询以获得想要的结果

【问题讨论】:

    标签: asp.net-mvc linq entity-framework entity-framework-4


    【解决方案1】:

    经过一番思考后,您推断出您需要一个有权执行关键操作之后的操作的用户列表:

    UserActionRights.Select(u => new
        { ActionId = u.ActionId,
          UserIds = UserActionRights.Where(u1 => u1.ActionId > u.ActionId)
            .Select(u1 => u1.UserId).ToList()
        }); 
    

    希望我能理解你。

    【讨论】:

      【解决方案2】:
      Users.GroupBy(u => u.Action.Id, u => u.Id)
      

      【讨论】:

        猜你喜欢
        • 2011-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多