如何使用LINQ 2用字典填充类 [英] How to populate a Class with Dictionary using LINQ 2
问题描述
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
用于将这些字符串转换为类型ObjectType
的Enum
.
每个字典项的值是被解析为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屋!