gpt4 book ai didi

c# - Linq 嵌套 group by 操作身份数据

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:35 24 4
gpt4 key购买 nike

嗨,几天来我一直在尝试将一种数据结构的形状更改为另一种,但没有成功。

原始数据结构如下。

public class Role : IdentityRole
{
public string Name { get; set; }
public virtual List<PermissionInRole> PermissionsInRole { get; set; }
}

public class PermissionInRole
{
public Guid Id { get; set; }
public virtual Permission Permission { get; set; }
public virtual Feature Feature { get; set; }
}

public class Permission
{
public Guid Id { get; set; }

public string Description { get; set; }
}

public class Feature
{
public Guid Id { get; set; }

public string Name { get; set; }

public virtual Application Application { get; set; }
}

public class Application
{
public Guid Id { get; set; }

public string Name { get; set; }

public virtual List<Feature> Features { get; set; }
}

我想用 Linq 把它改成这个形状。

public class ApplicationApiModel
{
public string Name { get; set; }

public ICollection<FeaturesApiModel> Features { get; set; }
}

public class FeaturesApiModel
{
public string Name { get; set; }

public IEnumerable<PermissionsApiModel> Permissions { get; set; }
}

public class PermissionsApiModel
{
public string PermissionName { get; set; }

public IEnumerable<Role> Role { get; set; }
}

public class Role
{
public string Name { get; set; }
}

我想要一个应用程序集合,其中包含一个功能集合,其中包含一个权限集合,其中包含一个具有父权限的角色集合。

我确信这可以通过 linq 来完成,但是我没有管理任何帮助会很棒。

最佳答案

经过多次尝试,我最终得到了以下工作代码

            var query = from role in roles
group role by role.PermissionsInRole
into g
select new
{
PinR = g.Key,
role = g.ToList()
};

var transferList = (from w in query
from pr in w.PinR
select new
{
Feature = pr.Feature, Permission = pr.Permission, TransferRole = w.role.Single()
})
.ToList()
.GroupBy(o => o.Feature, (key, o) =>
new FeaturesApiModel
{
Name = key.Name,
Permissions = o.GroupBy(transferObject => transferObject.Permission, (subKey,transferObject) =>
new PermissionsApiModel
{
PermissionName = subKey.Description,
Role = transferObject.Select(flatTransferObject => new RoleAPIModel {Name = flatTransferObject.TransferRole.Name})
}
)
});

关于c# - Linq 嵌套 group by 操作身份数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27481120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com