0

I have an data object like below :

public class RootObject
{ 
  public string ticker {get;set;}
  public List<Result> results {get;set;} 
}

and Result Object is like :

{
  public double open {get;set;}
  public double close {get;set;}
}

Now every time I will be multiple result for same ticker. So I want to add all result elements into same ticker. Instead of create it to multiple times into list.

public static List<RootObject> minuteAggregateList = new List<RootObject>();
public void historicalMinuteAggData(string symbol)
        {
            int daysCount = 0;
            for(int i=1; i<=20; i++)
            {
                DateTime date = DateTime.Now.Date.AddDays(-i);
                if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
                {
                    daysCount++;
                    var startUnixTime = (date.Add(new TimeSpan(13, 30, 00)).Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
                    var endUnixTime = (date.Add(new TimeSpan(20, 00, 00)).Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
                    using (var reader = new StreamReader(new WebClient().OpenRead(string.Format("API For Fetch Data"))))
                    {
                        var x = reader.ReadLine();
                        RootObject data = JsonConvert.DeserializeObject<RootObject>(x);
                        minuteAggregateList.Add(data);
                    }
                }
                if (daysCount == 7)
                    break;
            }
        }

Right now it's giving us multiple Results for same ticker in minuteAggregateList. How ever I need like if AAPL is in list then add new element in nested Result of AAPL. So how to make it possible to add elements in nested position under particular ticker ?

1
  • Use a dictionary making the key the unique item in the ticker. Then look up if dictionary exists. To handle multiple items make the value of the dictionary a List() object. Commented Apr 1, 2019 at 15:06

2 Answers 2

1

from below code you will be able to add result list in existing tick list and also it will be allow you to place list at your selected position.

public static List<RootObject> minuteAggregateList = new List<RootObject>();
public void historicalMinuteAggData(string symbol)
        {
            int daysCount = 0;
            for(int i=1; i<=20; i++)
            {
                DateTime date = DateTime.Now.Date.AddDays(-i);
                if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
                {
                    daysCount++;
                    var startUnixTime = (date.Add(new TimeSpan(13, 30, 00)).Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
                    var endUnixTime = (date.Add(new TimeSpan(20, 00, 00)).Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
                    using (var reader = new StreamReader(new WebClient().OpenRead(string.Format("API For Fetch Data"))))
                    {
                        var x = reader.ReadLine();
                        RootObject data = JsonConvert.DeserializeObject<RootObject>(x);
                        if (chk != null)
                        {
                            minuteAggregateList.Single(q => q.ticker == symbol).results.InsertRange(0, data.results);
                        }
                        else
                            minuteAggregateList.Add(data);
                    }
                }
                if (daysCount == 7)
                    break;
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

1

before adding data to list, need to check same ticker value.

public static List<RootObject> minuteAggregateList = new List<RootObject>();
public void historicalMinuteAggData(string symbol)
        {
            int daysCount = 0;
            for(int i=1; i<=20; i++)
            {
                DateTime date = DateTime.Now.Date.AddDays(-i);
                if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
                {
                    daysCount++;
                    var startUnixTime = (date.Add(new TimeSpan(13, 30, 00)).Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
                    var endUnixTime = (date.Add(new TimeSpan(20, 00, 00)).Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
                    using (var reader = new StreamReader(new WebClient().OpenRead(string.Format("API For Fetch Data"))))
                    {
                        var x = reader.ReadLine();
                        RootObject data = JsonConvert.DeserializeObject<RootObject>(x);
                        if(minuteAggregateList.Any(node => node.ticker == data.ticker))
                        {
                             minuteAggregateList.Where(node => node.ticker == data.ticker)
                                                .Select(val => {
                                                      val.results.AddRange(data.results); 
                                                      return val;
                                                        }).ToList();
                        }
                        else
                        {
                             minuteAggregateList.Add(data);
                        }
                    }
                }
                if (daysCount == 7)
                    break;
            }
        }

To add data to matched list, first need to select matched ticker (i.e using Where()). And than after in Select() add result data. After that changes are need to be apply on existing list using ToList().

2 Comments

from where Val we will get ? for line "val.results.AddRange(data.results);"
Sorry for missing code. Update my Code. Val value comes from lambda expression of select item which value satisfied condition in Where().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.