简体   繁体   English

C#-将匿名类型解析为int32

[英]C# - Parse anonymous type to int32

I'm having problems when trying to parse values that I retrieve from an XML-file. 尝试解析从XML文件检索的值时遇到问题。 I get several values stored toList() in a variable var and I want to convert them to Int32 and summarize them. 我将几个值存储到变量var中的toList()中,我想将它们转换为Int32并进行汇总。 Here is the method I am using: 这是我正在使用的方法:

public void ViktTjurar()
{
    string år = TextBoxÅr.Text;
    int test= 0;
    int summa = 0;
    XElement vikt =  XElement.Load(path);


    var vikttjurar = (from h in vikt.Descendants("älgrapport")
                      where (string)h.Element("år") == år && (string)
                      h.Element("typ") == "Tjur"
                      select new
                      {
                          tvikt = int.Parse(h.Element("Vikt").Value),

                      }).ToList();

    //List<int> vikter1 = new List<int>();
    foreach (var e in vikttjurar)
    {
        test = Convert.ToInt32(e);                             
    }
} 

At the moment I get 3 values from the XML-file, stored in var vikttjurar and I thought I parsed them correctly with: tvikt = int.Parse(h.Element("Vikt").Value) . 目前,我从存储在var vikttjurar的XML文件中获得了3个值,并且我认为我可以使用tvikt = int.Parse(h.Element("Vikt").Value)正确地解析它们。

It seems it doesn't work. 似乎不起作用。 When assigning the value of e to test (in a foreach loop) I get error saying: 当将e的值分配给test (在foreach循环中)时,出现错误消息:

Cannot implicitly convert type 'AnonymousType#1' to 'int' 无法将类型'AnonymousType#1'隐式转换为'int'

So I tried to Convert e to Int32 (as you can see in my code above) but that wont work either and I get the same error: System.InvalidCastException . 因此,我尝试Convert e ConvertInt32 (如您在上面的代码中看到的),但是那也不起作用,并且我得到了相同的错误: System.InvalidCastException

Does anyone know how to solve the parsing from anonymous type to int or, even better, parse AND summarize the values in var vikttjurar ? 有谁知道如何解决从匿名类型到int的解析,或者甚至更好地解析并汇总var vikttjurar的值?

You've already done the conversion in your linq statement, and created anonymous objects with a single property, tvikt . 您已经在linq语句中完成了转换,并创建了具有单个属性tvikt匿名对象。 Now you just need to look it up: 现在,您只需要查找它:

    foreach (var e in vikttjurar)
    {

        test = e.tvikt;

    }

BTW, the XElement type supports explicit conversion to Int32 (or int ), so in your Linq statement, you can: 顺便说一句, XElement类型支持显式转换Int32 (或int ),因此在您的Linq语句中,您可以:

...select new
{
    tvikt = (int)h.Element("Vikt"),

}...

Just do this, you have the int parsed already: 这样做,您已经解析了int

test = e.tvikt;

Also, you could simplify your whole code a bit: 另外,您可以简化整个代码:

var vikttjurar = (from h in vikt.Descendants("älgrapport")
                  where (string)h.Element("år") == år && (string)
                  h.Element("typ") == "Tjur"
                  select int.Parse(h.Element("Vikt").Value))
                 .ToList();

//List<int> vikter1 = new List<int>();
foreach (var e in vikttjurar)
{
    test = e; // e is an int here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM