如何将旧数据类型值与新数据值

时间:2017-01-23 07:40:46

标签: c# json linq api console-application

所以我使用这个API来显示当前的价格以及其他涉及到以太坊硬币的东西。我正在尝试创建一个小型控制台应用程序,用于检查上次扫描时值是否发生了变化。

到目前为止我所拥有的是......我知道我正在用当前值扫描当前值,所以显然它永远不会改变。 我尝试设置一个变量来保存旧值但是没有做任何事情。

如何比较第一次扫描和第二次扫描以查看浮动值是上升还是下降?

private static void Ticker()
        {
            while (true)
            {
                const string uri = @"https://api.coinmarketcap.com/v1/ticker/ethereum/";
                var client = new WebClient();
                var content = client.DownloadString(uri);

                var results = JsonConvert.DeserializeObject<List<CoinApi>>(content);

                float currentAmount = results[0].price_usd;
                if (currentAmount < currentAmount)
                {
                    Console.WriteLine("Ammount is lower than the last time.");
                }
                else if (currentAmount > currentAmount)
                {
                    Console.WriteLine("The amount is higher than the last time.");
                }
                else if (currentAmount == currentAmount)
                {
                    Console.WriteLine("The amount hasnt changed since the last time we scanned.");
                }
            }
        }

这是类文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CryptoTicker.Core
{
    class Main
    {
    }

    public class CoinApi
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Symbol { get; set; }
        public float price_usd { get; set; }
    }
}

4 个答案:

答案 0 :(得分:0)

您需要记住以前的值。这样做的一种方法是维护id和#CoinApi类的字典

1.<UploadAsync>d__84.MoveNext()  --- End of stack trace from previous location where exception was thrown ---     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)     at System.Runtime.CompilerServices.TaskAwaiter

然后,您可以将当前值存储在此字典中,然后将新值与此现有值进行比较。然后更新为新值

public Dictionary<int, CoinApi> CoinDict = new Dictionary<int, CoinAPI>();

答案 1 :(得分:0)

尝试将旧值保存到静态变量应解决您的问题。 TODOS:你第一次要求你会怎么做?处理异常等......

其他问题:这应该在while(true)中运行吗?也许你应该使用HttpClient并重复使用它。

    private static float _oldValue;

    private static void Ticker()
    {
        while (true)
        {
            const string uri = @"https://api.coinmarketcap.com/v1/ticker/ethereum/";
            var client = new WebClient();
            var content = client.DownloadString(uri);

            var results = JsonConvert.DeserializeObject<List<CoinApi>>(content);

            float currentAmount = results[0].price_usd;
            if (currentAmount < _oldValue)
            {
                Console.WriteLine("Ammount is lower than the last time.");
            }
            else if (currentAmount > _oldValue)
            {
                Console.WriteLine("The amount is higher than the last time.");
            }
            else if (currentAmount == _oldValue)
            {
                Console.WriteLine("The amount hasnt changed since the last time we scanned.");
            }

            _oldValue = currentAmount;
        }
    }

答案 2 :(得分:0)

正如你所写的那样,currentAmount总是等于它自己。

这应该有效:

  public function actionYourAction()
  {
      $post = Yii::$app->request->post();

      if (!empty($post)){

          $postTags =  $post['tags'];
            foreach ($postASCCId as $key => $value) {

                  $modelNew = new YourModel();

                  $modelNew->tag = $value;

                  $modelNew->save();
          }
      }  
      .....
  } 

答案 3 :(得分:0)

您必须将先前的执行值传递给当前执行才能进行比较。 将该变量声明为该类中的全局分数。或者将其声明到您想要使用它的级别。