首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GroupBy后GroupBy

GroupBy后GroupBy
EN

Stack Overflow用户
提问于 2022-10-26 08:30:37
回答 2查看 52关注 0票数 -1

我有以下DTO:

代码语言:javascript
复制
class Permission
{
    public string ObjectId { get; set; }
    public string ObjectType { get; set; }
    public string Scope { get; set; }
    public string? AccountId { get; set; }
    public string? GroupId { get; set; }
}

var permissions = new List<Permission>()
{
    new(){ ObjectId = "1", ObjectType = "link", Scope = "link:read", AccountId = "1", GroupId = null },
    new(){ ObjectId = "1", ObjectType = "link", Scope = "link:read", AccountId = "2", GroupId = null },
    new(){ ObjectId = "1", ObjectType = "link", Scope = "link:read", AccountId = null, GroupId = "1" },
    new(){ ObjectId = "1", ObjectType = "link", Scope = "link:write", AccountId = "2", GroupId = null },
    new(){ ObjectId = "2", ObjectType = "link", Scope = "link:read", AccountId = "1", GroupId = null },
};

我希望得到以下结果:

代码语言:javascript
复制
{[
    "1": {
        "read": {
            "accounts": ["1", "2"],
            "groups": ["1", "2", "3"]
        },
        "write": {
            "accounts": ["1", "2"],
            "groups": ["1", "2", "3"]
        }
  }
]}

基本上是Permission的一个对象数组,按其ObjectId属性分组,然后按每个包含AccountIdGroupId属性数组的Scope进行分组。

每个Permission可以有相同的ObjectId,但是有不同的ScopeAccountIdGroupId

我尝试使用GroupBy,但这给了我一个IGrouping,我不知道如何继续。

EN

回答 2

Stack Overflow用户

发布于 2022-10-26 08:59:04

您需要的不仅仅是嵌套的组bys。在您的输出JSON中,您有键作为属性名。您还需要使用ToDictionary将值转换为属性键。

代码语言:javascript
复制
var permissions = new List<Permission>()
        {
            new(){ ObjectId = "1", ObjectType = "link", Scope = "link:read", AccountId = "1", GroupId = null },
            new(){ ObjectId = "1", ObjectType = "link", Scope = "link:read", AccountId = "2", GroupId = null },
            new(){ ObjectId = "1", ObjectType = "link", Scope = "link:read", AccountId = null, GroupId = "1" },
            new(){ ObjectId = "1", ObjectType = "link", Scope = "link:write", AccountId = "2", GroupId = null },
            new(){ ObjectId = "2", ObjectType = "link", Scope = "link:read", AccountId = "1", GroupId = null },
        };
        var result = permissions.GroupBy(r=> r.ObjectId).Select(r=> new {
            r.Key,
            InnerGroups = r.GroupBy(q=> q.Scope.Replace("link:","")).Select(q=> new {
                Scope = q.Key,
                Accounts =  q.Where(z=> z.AccountId != null).Select(z=> z.AccountId).ToArray(),
                Groups = q.Where(z=> z.GroupId != null).Select(z=> z.GroupId).ToArray()
            })
        })
            .ToDictionary(r=> r.Key,r=> r.InnerGroups.ToDictionary(q=> q.Scope,q=> new {q.Accounts,q.Groups}));
        
        var serialized = JsonSerializer.Serialize(result,new JsonSerializerOptions{ WriteIndented=true });

这是输出:

代码语言:javascript
复制
 {
  "1": {
    "read": {
      "Accounts": [
        "1",
        "2"
      ],
      "Groups": [
        "1"
      ]
    },
    "write": {
      "Accounts": [
        "2"
      ],
      "Groups": []
    }
  },
  "2": {
    "read": {
      "Accounts": [
        "1"
      ],
      "Groups": []
    }
  }
}

小提琴

票数 1
EN

Stack Overflow用户

发布于 2022-10-26 09:00:55

您必须使用嵌套的GroupBy来实现这一点,然后将结果转换为字典:

代码语言:javascript
复制
var result = permissions
    .GroupBy(p => p.ObjectId)
    .ToDictionary(g => g.Key, g => g.GroupBy(g => g.Scope)
    .ToDictionary(g => g.Key, g => new
    {
        accounts = g.Select(per => per.AccountId).Distinct().ToList(),
        groups = g.Select(per => per.GroupId).Distinct().ToList()
    }));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74204742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档