如何使用LINQ 2用字典填充类 [英] How to populate a Class with Dictionary using LINQ 2

查看:62
本文介绍了如何使用LINQ 2用字典填充类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与此处解决的问题有关:如何使用LINQ用词典填充类

尝试填充Dictionary<ObjectType, float>时遇到另一个问题,其中ObjectType是我创建的自定义枚举.

LootProfile.cs

public class LootProfile
{

    /*
    dynamicDrop multipli non definiti
    */
    public string name;

    public int dynamicDropNumber; //  = 3 
    public Dictionary<int, float> dynamicDrop;  // 70, 40, 10
    public Dictionary<ObjectType, float> dynamicType;
    public Dictionary<Rarity, float> dynamicDropRarity; // "Common" - 60, "Uncommon" - 26, "Rare" - 12, "Epic" - 2

    public int staticDropNumber; // = 2
    public Dictionary<int, Dictionary<int, float>> staticDrop;  // 0 - idPattern - prob

    public Faction faction;
    public Location location;
}

ImporterXML

var query = from item in xml.Root.Elements("LootProfile")
                select new LootProfile()
                {

                    name = (string)item.Attribute("name"),
                    dynamicDropNumber = (int)item.Element("dynamicDropNumber"),
                    dynamicDrop = item.Elements("dynamicDrop")
                        .Select((Item, Index) => new { Item, Index })
                        .ToDictionary(x => x.Index, x => float.Parse(x.Item.Value)),
                    dynamicType = item.Elements("dynamicTypeArmor")
                        .Select((Item, Index) => new { Item, Index })
                        .ToDictionary(x => x.Index, x => float.Parse(x.Item.Value))
                }
return query.ToList<LootProfile>();

我的问题是如何在字典中导入XML元素的值

dynamicTypeArmor

dynamicTypeWeapon

dynamicTypeConsumable 

在同一词典中

ObjectType.Armor for dynamicTypeArmor

ObjectType.Weapon for dynamicTypeWeapon

ObjectType.Consumable for dynamicTypeConsumable

解决方案

以下是您的操作方法:

var query = xml.Elements("LootProfile")
    .Select(item => new LootProfile()
    {
        //...
        dynamicType =
            item.Elements()
                .Where(x => x.Name.LocalName.StartsWith("dynamicType"))
                .ToDictionary(
                    x => (ObjectType)Enum.Parse(
                        typeof(ObjectType),
                        x.Name.LocalName.Substring("dynamicType".Length)),
                    x => float.Parse(x.Value))
        //...
    });

使用Where仅选择名称以"dynamicType"开头的元素,然后从数据中创建字典.

每个字典项的键是除去"dynamicType"后相应元素名称的其余部分.这将为您提供装甲",武器"或消耗品". Enum.Parse用于将这些字符串转换为类型ObjectTypeEnum.

每个字典项的值是被解析为float的相应元素的值.

related to the problem solved here: How to populate a Class with Dictionary using LINQ

I have another problem trying to fullfill a Dictionary<ObjectType, float> where ObjectType is a custom Enum i've create.

LootProfile.cs

public class LootProfile
{

    /*
    dynamicDrop multipli non definiti
    */
    public string name;

    public int dynamicDropNumber; //  = 3 
    public Dictionary<int, float> dynamicDrop;  // 70, 40, 10
    public Dictionary<ObjectType, float> dynamicType;
    public Dictionary<Rarity, float> dynamicDropRarity; // "Common" - 60, "Uncommon" - 26, "Rare" - 12, "Epic" - 2

    public int staticDropNumber; // = 2
    public Dictionary<int, Dictionary<int, float>> staticDrop;  // 0 - idPattern - prob

    public Faction faction;
    public Location location;
}

ImporterXML

var query = from item in xml.Root.Elements("LootProfile")
                select new LootProfile()
                {

                    name = (string)item.Attribute("name"),
                    dynamicDropNumber = (int)item.Element("dynamicDropNumber"),
                    dynamicDrop = item.Elements("dynamicDrop")
                        .Select((Item, Index) => new { Item, Index })
                        .ToDictionary(x => x.Index, x => float.Parse(x.Item.Value)),
                    dynamicType = item.Elements("dynamicTypeArmor")
                        .Select((Item, Index) => new { Item, Index })
                        .ToDictionary(x => x.Index, x => float.Parse(x.Item.Value))
                }
return query.ToList<LootProfile>();

My problem is how to import in the Dictionary the value of the XML Element

dynamicTypeArmor

dynamicTypeWeapon

dynamicTypeConsumable 

in the same Dictionary with

ObjectType.Armor for dynamicTypeArmor

ObjectType.Weapon for dynamicTypeWeapon

ObjectType.Consumable for dynamicTypeConsumable

解决方案

Here is how you can do it:

var query = xml.Elements("LootProfile")
    .Select(item => new LootProfile()
    {
        //...
        dynamicType =
            item.Elements()
                .Where(x => x.Name.LocalName.StartsWith("dynamicType"))
                .ToDictionary(
                    x => (ObjectType)Enum.Parse(
                        typeof(ObjectType),
                        x.Name.LocalName.Substring("dynamicType".Length)),
                    x => float.Parse(x.Value))
        //...
    });

You use Where to select only elements of which name starts with "dynamicType", and then you create a dictionary out of the data.

The key of each dictionary item is the rest of the name of the corresponding element after removing "dynamicType". This will give you "Armor", "Weapon", or "Consumable". Enum.Parse is used to convert these strings into an Enum of type ObjectType.

The value of each dictionary item is the value of the corresponding element parsed as a float.

这篇关于如何使用LINQ 2用字典填充类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆