【问题标题】:How To Convert IEnumerable To one object如何将 IEnumerable 转换为一个对象
【发布时间】: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


【解决方案1】:

您可以从Linq 中使用AnySelectMany() 的组合,

喜欢,

    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()
        }
    };

【讨论】:

  • 你应该使用 any 而不是 Select().Contains()
  • 我没有收到你的评论
  • 可以自动生成结果而不调用每个属性
  • 不,无法自动定义此属性。因为要为result 对象中的属性赋值,您需要执行上述操作
【解决方案2】:

对于公式和产品列表,您可以使用 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 .... 也许您应该将所有规则分组并加入每个 boolData 的公式/产品

【讨论】:

    【解决方案3】:

    您可以使用以下代码:

    • SelectMany 将多个产品/公式列表合并为一个
    • distinct 删除重复项
    • OrderBy 确保升序
    • 任何检查是否至少有一个 CanBuy/CanSale 属性为真

    顺便说一句 - 我同意 cmets 说类/属性名称可能是

        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,
            }
        };
    

    【讨论】:

      猜你喜欢
      • 2013-07-04
      • 1970-01-01
      • 2016-03-24
      • 2012-06-02
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多