如何将 IEnumerable 转换为一个对象

How To Convert IEnumerable To one object

在我的应用程序中,用户可以拥有多个角色,我想将角色列表转换为一个通用角色对象以申请权限

我如何处理将 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, }

                }
            };

您可以使用 AnySelectMany() 的组合,来自 Linq

喜欢,

    var Result = new Role()
    {
        ListBools = new boolData()
        {
            CanBuy = UserRoles.Any(x => x.ListBools.CanBuy),
            CanSale = UserRoles.Any(x => x.ListBools.CanSale)
        },
        ListIds = new ListId()
        {
            Product = UserRoles.SelectMany(x => x.ListIds.Product).Distinct(),
            Formula = UserRoles.SelectMany(x => x.ListIds.Formula).Distinct()
        }
    };

对于公式和产品列表,您可以使用 SelectMany + Distinct:

  Role sum = new Role
        {
            ListBools = new boolData()
            {
                CanBuy = true,
                CanSale = true
            },
            ListIds = new ListId
            {
                Formula = UserRoles.SelectMany(x => x.ListIds.Formula).Distinct().ToList(),
                Product = UserRoles.SelectMany(x => x.ListIds.Product).Distinct().ToList()
            }
        };

我不明白 ListBools 应该如何产生....也许你应该将所有规则分组并加入 Formula/Product per boolData

您可以使用此代码:

  • SelectMany 将许多 Product/Formula 个列表合并为一个
  • Distinct 删除重复项
  • OrderBy 确保升序
  • A​​ny 检查是否至少有一个 CanBuy/CanSale 属性 为真

顺便说一句 - 我同意 Class/Property 名称可以是

的评论
    var products = UserRoles.SelectMany(m => m.ListIds.Product).Distinct().OrderBy(m => m).ToList();
    var formulas = UserRoles.SelectMany(m => m.ListIds.Formula).Distinct().OrderBy(m => m).ToList();
    var anyCanBuy = UserRoles.Any(m => m.ListBools.CanBuy);
    var anyCanSale = UserRoles.Any(m => m.ListBools.CanSale);

    Role Result = new Role()
    {
        ListBools = new boolData()
        {
            CanBuy = anyCanBuy,
            CanSale = anyCanSale,
        },
        ListIds = new ListId()
        {
            Product = products,
            Formula = formulas,
        }
    };