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

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

问题描述

在项目中,我使用XML导入类的各种实例。
我需要在列表导入。
的问题是如何导入所有的dynamicDrop在词典:



XML:

 < LootProfile NAME =volpeNormale> 
将; dynamicDropNumber→3&下; / dynamicDropNumber>
< dynamicDrop> 70℃/ dynamicDrop>
将; dynamicDrop> 40℃/ dynamicDrop>
< dynamicDrop大于10< / dynamicDrop>
< dynamicTypeArmor> 33 LT; / dynamicTypeArmor>
< dynamicTypeWeapon> 33 LT; / dynamicTypeWeapon>
< dynamicTypeConsumable> 34 - ; / dynamicTypeConsumable>
< dynamicRarityCommon> 70℃/ dynamicRarityCommon>
将; dynamicRarityUncommon> 20℃/ dynamicRarityUncommon>
< dynamicRarityRare> 8示/ dynamicRarityRare>
将; dynamicRarityEpic→2&下; / dynamicRarityEpic>
将; staticDropNumber→2&下; / staticDropNumber>
< staticDrop idPattern =100> 60℃/ staticDrop>
将; staticDrop idPattern =145> 100℃/ staticDrop>
<另一派>所有< /派>
<地点>&所有LT; /地点>
< / LootProfile>



XMLImporter查询:

  VAR的查询=从xml.Root.Elements(LootProfile)
选择新LootProfile()
{
NAME =(字符串)item.Attribute(项目 NAME),
dynamicDropNumber =(INT)item.Element(dynamicDropNumber),
dynamicDrop =(词典< INT,串>)item.Element(dynamicDrop)//这一个不工作!
//其他元素....
}
返回query.ToList< LootProfile>();


解决方案

下面是你如何做到这一点:

  VAR的查询= xml.Elements(LootProfile)
。选择(项目=>新建LootProfile()
{
NAME =(字符串)item.Attribute(名称) ,
dynamicDropNumber =(INT)item.Element(dynamicDropNumber),
dynamicDrop =
item.Elements(dynamicDrop)
。选择((项,指数)= >新{ITEM,指数})
.ToDictionary(X => x.Index,X => float.Parse(x.Item.Value))
//其他元素....
});

VAR的结果= query.ToList();



诀窍是使用的 选择超载 ,给你两个拉姆达参数;项目本身,以及该项目的索引


In my project I use an XML to import various instances of a class. I need to import in a List. The problem is how to import all "dynamicDrop" in a Dictionary:

XML:

<LootProfile name="volpeNormale">
    <dynamicDropNumber>3</dynamicDropNumber>
    <dynamicDrop>70</dynamicDrop>
    <dynamicDrop>40</dynamicDrop>
    <dynamicDrop>10</dynamicDrop>
    <dynamicTypeArmor>33</dynamicTypeArmor>
    <dynamicTypeWeapon>33</dynamicTypeWeapon>
    <dynamicTypeConsumable>34</dynamicTypeConsumable>
    <dynamicRarityCommon>70</dynamicRarityCommon>
    <dynamicRarityUncommon>20</dynamicRarityUncommon>
    <dynamicRarityRare>8</dynamicRarityRare>
    <dynamicRarityEpic>2</dynamicRarityEpic>
    <staticDropNumber>2</staticDropNumber>
    <staticDrop idPattern="100">60</staticDrop>
    <staticDrop idPattern="145">100</staticDrop>
    <faction>All</faction>
    <location>All</location>
</LootProfile>

XMLImporter query:

var query = from item in xml.Root.Elements("LootProfile")
            select new LootProfile()
            {
                name = (string)item.Attribute("name"),
                dynamicDropNumber = (int)item.Element("dynamicDropNumber"),
                dynamicDrop = (Dictionary<int,string>)item.Element("dynamicDrop) //this one doesnt work!
                //other element....
            }
return query.ToList<LootProfile>();

解决方案

Here is how you can do it:

var query = xml.Elements("LootProfile")
    .Select(item => 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))
        //other element....
    });

var result = query.ToList();

The trick is to use an overload of Select that gives you two lambda parameters; the item itself, and the index of the item.

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

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