-2

I have the following:-

double v = 1.1;
Dictionary<double,double> cleanedTimes = new Dictionary<double,double>();
//code for building the cleanedTimes goes here..

so how i can check if the v is between any the pair values inside the cleanedTimes items?

Thanka

John John
  • 1
  • 72
  • 238
  • 501
  • @Steve i meant to say double – John John Mar 01 '21 at 20:02
  • 1
    define "pair values" please. – Guru Stron Mar 01 '21 at 20:03
  • 1
    Please provide an example of how your dictionary is initialized and an example of the positive result as well one of the negative result expected – Steve Mar 01 '21 at 20:03
  • 1
    Does this answer your question? [What is the best way to iterate over a dictionary?](https://stackoverflow.com/questions/141088/what-is-the-best-way-to-iterate-over-a-dictionary) – Peter B Mar 01 '21 at 20:04
  • From the question wording I would say something like `v >= cleanedTimes.Values.Min() && v <= cleanedTimes.Values.Max()` – Guru Stron Mar 01 '21 at 20:06
  • 1
    It appears your using a dictionary just because it returns to you a KeyValuePair. It doesn't appear you will be using the Key for anything, you should really consider just using a better data-structure such as an IEnumerable<(double, double)> or some other abstraction – johnny 5 Mar 01 '21 at 20:35

1 Answers1

0

You can use Any() with any condition you like. Your condition would be as simple as "bigger than the key, but smaller than the value or the other way round". So the expression/code can look like this:

IDictionary<double, double> values = new Dictionary<double, double>();
values.Add(1.1, 2.2);
values.Add(3.3, 4.4);
values.Add(5.5, 5.0);

double check = 1.5;

bool found = values.Any(it => (it.Key <= check && check <= it.Value) ||
                              (it.Value <= check && check <= it.Key));
Console.WriteLine(found);

This will print True as the value check of 1.5 is between 1.1 and 2.2.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • I thinnk it should be `Any(it => check >= it.Key && check <= it.Value);` if i translate the requirement _"between any the pair values"_ correctly, because i think "pair values" means that he stores the pair in the key and in the value. – Tim Schmelter Mar 01 '21 at 20:21
  • @TimSchmelter That's what I wrote? Or did I miss something? – Progman Mar 01 '21 at 20:31
  • @TimSchmelter Can you check https://dotnetfiddle.net/pTPBos, am I missing something? I don't get what you are saying. – Progman Mar 01 '21 at 20:41
  • Your first condition is correct, i got it wrong first time. But why you need the second? – Tim Schmelter Mar 01 '21 at 21:02
  • @TimSchmelter The `key` might be bigger than the `value` as seen in the 3rd example KeyValuePair, but the value to search for might still be between these numbers. – Progman Mar 01 '21 at 21:03
  • ok, but op didnt mention this. I think that this would be a bug. If you have a collection with two properties where the first stores the min- and the second stores the max-value, i would not handle the case that somehow wrong values were stored in each property. At least not where i evaluate the collection but where i fill it. – Tim Schmelter Mar 01 '21 at 21:06
  • 1
    @TimSchmelter OP didn't mention that the `key` is always smaller than the `value` either. The conditions makes sure that both "directions" are checked. – Progman Mar 01 '21 at 21:14