在 C# 中获取要相加的元素

发布于 2024-10-12 02:55:11 字数 1614 浏览 3 评论 0原文

我有以下代码 -

private static void convert()
{                 
  webservice.Sum[] test = new webservice.Sum[1];         
  webservice.feed CallWebService = new webservice.feed();          
  foreach(XElement el in turnip.Descendants("row"))         
    {             
    test[0].person = el.Descendants("var").Where
         (
         x => (string)x.Attribute("name") == "person"
         ).SingleOrDefault().Attribute("value").Value;

    test[0].time = System.Convert.ToInt32(el.Descendants("var").Where
         (
          x => (string)x.Attribute("name") == "time"
         ).SingleOrDefault().Attribute("value").Value);

    test[0].erase = System.Convert.ToInt32(el.Descendants("var").Where
         (
          x => (string)x.Attribute("name") == "erase"
          ).SingleOrDefault().Attribute("value").Value);

    test[0]. available = el.Descendants("var").Where
         (
          x => (string)x.Attribute("name") == "available"
          ).SingleOrDefault().Attribute("value").Value;             

    test[0].external = el.Descendants("var").Where
         (
          x => (string)x.Attribute("name") == "external"
          ).SingleOrDefault().Attribute("value").Value;    
    CallWebService.updateFeed(test, year);
      } 
}

我需要做的是让测试数组中的“人”和“时间”元素分别相加。例如,它们是从 csv 中读取的,因此如果 csv 的帐户的值为 、 10、20 和 30。我希望它将帐户显示为具有所需的值 60。然后与擦除相同。这些将是运行时在表单中显示的所需值,然后将从 Web 服务返回人员值,如果帐户返回 40 而不是 60,则将显示发生错误。

现在澄清一下,我正在从网络服务获得响应,但它并不总是返回所需的值。来自网络服务的结果在网络表单上返回,我想在表单上获得来自网络服务的结果(我有)以及结果应该是什么,以比较或确认结果是否正确。因此,在运行时,表单会告诉我 csv 中帐户的添加值是多少,然后是 Web 服务结果返回的值。

I have the following code -

private static void convert()
{                 
  webservice.Sum[] test = new webservice.Sum[1];         
  webservice.feed CallWebService = new webservice.feed();          
  foreach(XElement el in turnip.Descendants("row"))         
    {             
    test[0].person = el.Descendants("var").Where
         (
         x => (string)x.Attribute("name") == "person"
         ).SingleOrDefault().Attribute("value").Value;

    test[0].time = System.Convert.ToInt32(el.Descendants("var").Where
         (
          x => (string)x.Attribute("name") == "time"
         ).SingleOrDefault().Attribute("value").Value);

    test[0].erase = System.Convert.ToInt32(el.Descendants("var").Where
         (
          x => (string)x.Attribute("name") == "erase"
          ).SingleOrDefault().Attribute("value").Value);

    test[0]. available = el.Descendants("var").Where
         (
          x => (string)x.Attribute("name") == "available"
          ).SingleOrDefault().Attribute("value").Value;             

    test[0].external = el.Descendants("var").Where
         (
          x => (string)x.Attribute("name") == "external"
          ).SingleOrDefault().Attribute("value").Value;    
    CallWebService.updateFeed(test, year);
      } 
}

What I need to do is to get the 'person' and 'time' elements in the test array to add up seperately. For instance they are getting read in from a csv, so if the csv had account with the values of , 10, 20 and 30. I would want it to show account as having the desired value of 60. Then the same with erase. These would be the desired values to be presented in the form when run, then the person values would be returned from the web service, if it came back 40 for account and not 60, that would show an error occuring.

To make it clear right now I am getting a response from the web service, however it will not always be the desired value returned. The reulst from the webservice are returned on a web form, I want to have the results from the web service on the form (which I have) and what the results should definately be to compare or confirm the reults are correct. So on run time the form will tell me what the added up values of account are from the csv, then what the web service results have returned.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-10-19 02:55:11

就像我通过聊天得到你的问题一样,我尝试解决你的问题。

这是如果你想获得整个 CSV 的总和:

private int SumOfCSV(string filePath)
{
        string csvContent = String.Empty;
        int returnValue = 0;

        try
        {
            StreamReader reader = new StreamReader(filePath);
            csvContent = reader.ReadToEnd();
            reader.Close();

            foreach(string value in csvContent.Split(';'))
            {
                returnValue += Convert.ToInt32(value);

            }               

        }
        catch (Exception)
        {
            Console.WriteLine("Reading " + Path.GetFileName(filePath) + " Failed.");
        }

        return returnValue;
}

Alex

like i got your problem via Chat i try to solve your problem.

This is if you want to get the sum of the whole CSV:

private int SumOfCSV(string filePath)
{
        string csvContent = String.Empty;
        int returnValue = 0;

        try
        {
            StreamReader reader = new StreamReader(filePath);
            csvContent = reader.ReadToEnd();
            reader.Close();

            foreach(string value in csvContent.Split(';'))
            {
                returnValue += Convert.ToInt32(value);

            }               

        }
        catch (Exception)
        {
            Console.WriteLine("Reading " + Path.GetFileName(filePath) + " Failed.");
        }

        return returnValue;
}

Alex

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文