Most recently started programming, faced with difficulty. I have a dictionary, inside which the keys are the names of the desired signals, inside each key there are 2 values, the time (such as DateTime) and the signal value at this point in time:

public static Dictionary<string, SortedDictionary<DateTime, double>> dic = new Dictionary<string, SortedDictionary<DateTime, double>>(); 

Here is the data in the dictionary:

 public static void readDic() { foreach (var varName in dic) { foreach (var data in varName.Value) { Console.WriteLine("{0}: Key: {1}, Value {2}", varName.Key, data.Key, data.Value); } } } 

Example output: Data output

How can I pull out a piece of data of all keys for a specified interval. for example, you need to display the data in 3 minutes, indicating the starting day, month, year, hour, minute

  • For some particular key outdoor dictionary? - PECHAPTER
  • Hi, you need to pull the data of all keys in the dictionary, if their values ​​fall into this interval of time - Ilya Chirkov
  • Gave the answer, but let's get it straight: it's more like trying to guess exactly what you need. If you did not guess, then we will do so. Describe the type of variable you want to get at the output. I decided that at the output you want a Dictionary <string, SortedDictionary <DateTime, double >> - just like at the input. - AK
  • These data are needed to transfer them as points to a zedgraph plot, x is time, y is a value, at a certain time interval. Ideally, it would be desirable for new dictionaries not to be created with this, so you need to somehow transfer these values ​​to the graph immediately, if possible. - Ilya Chirkov

1 answer 1

Not sure I understood what you want, but let's say in the first approximation.

We have the following data set:

 public Dictionary<string, SortedDictionary<DateTime, double>> GetSampleData() { var d1 = new SortedDictionary<DateTime, double>() { {DateTime.Today.AddDays(-4), -11.3}, {DateTime.Today.AddDays(-3), 10.6}, {DateTime.Today.AddDays(-2), 1.1}, {DateTime.Today.AddDays(-1), 2.4} }; var d2 = new SortedDictionary<DateTime, double>() { {DateTime.Today.AddDays(-3), 11.4} }; var d3 = new SortedDictionary<DateTime, double>() { { DateTime.Today.AddDays(-2), -11.3 }, { DateTime.Today.AddDays(-3), 4.8 } }; var d4 = new SortedDictionary<DateTime, double>() { { DateTime.Today.AddDays(0), 3.3 }, { DateTime.Today.AddDays(1), 6.2 } }; return new Dictionary<string, SortedDictionary<DateTime, double>>() { {"test1", d1 }, {"test2", d2 }, {"test3", d3 }, {"test4", d4 }, }; } 

enter image description here

Suppose your filter will be on such dates:

 var result = Filter(dic, DateTime.Today.AddDays(-3), DateTime.Today.AddDays(-1)); 

You want to get at the output:

 public Dictionary<string, SortedDictionary<DateTime, double>> Filter(Dictionary<string, SortedDictionary<DateTime, double>> source, DateTime from, DateTime to) { return source.Where(x => x.Value.Any(v => v.Key >= from && v.Key <= to)) .ToDictionary(x => x.Key, x => x.Value); } 

enter image description here

Arranges? Such a result wanted?

Okay, just another option. It is possible to understand the condition of the problem:

 public Dictionary<string, SortedDictionary<DateTime, double>> Filter(Dictionary<string, SortedDictionary<DateTime, double>> source, DateTime from, DateTime to) { return source.Where(x => x.Value.Any(v => v.Key >= from && v.Key <= to)) .ToDictionary(x => x.Key, x => new SortedDictionary<DateTime, double>(x.Value.Where(v => v.Key >= from && v.Key <= to).ToDictionary(y => y.Key, y => y.Value))); } 

enter image description here

  • Thank you very much for the answer, they helped me a lot - Ilya Chirkov