【发布时间】:2019-08-26 09:10:12
【问题描述】:
在我的应用程序中,用户可以拥有多个角色,我想将角色列表转换为一个通用角色对象以申请权限
如何处理将 List 转换为 Common 角色?
public class Role
{
public boolData ListBools { get; set; }
public ListId ListIds { get; set; }
}
public class boolData
{
public bool CanSale { get; set; }
public bool CanBuy { get; set; }
}
public class ListId
{
public IEnumerable<Int64> Product { get; set; }
public IEnumerable<Int64> Formula { get; set; }
}
例如,这个用户有两个我想要执行的角色,这些角色对一个角色对象是通用的
IEnumerable<Role> UserRoles = new List<Role>()
{
new Role(){ListBools = new boolData()
{
CanBuy = false,
CanSale = true
},
ListIds = new ListId()
{
Product = new List<long>(){1,4,58},
Formula = new List<long>(){2,7,}
}
},
new Role(){ListBools = new boolData()
{
CanBuy = true,
CanSale = true
},
ListIds = new ListId()
{
Product = new List<long>(){1,6,70},
Formula = new List<long>(){2,9,}
}
},
};
必须转换为
Role Result = new Role()
{
ListBools = new boolData()
{
CanBuy = true,
CanSale = true
},
ListIds = new ListId()
{
Product = new List<long>() { 1, 4, 6, 58, 70 },
Formula = new List<long>() { 2, 7, 9, }
}
};
【问题讨论】:
-
你做了什么来手动达到预期的结果?如果你能弄清楚,把它放到代码中,你就拥有了。无关:命名不佳。帮自己一个忙,重新考虑名字。此外,类应以大写字母开头 ("boolData" => "BoolData")
-
我建议使用角色对象列表或使用 var 代替角色类型将结果存储到对象中。
标签: c# linq ienumerable