Solved

Looping in Dictionary using c#

Posted on 2015-01-02
3
141 Views
Last Modified: 2015-01-07
Hi Experts,

I have a dictionary with key,value  both int values (dictionary<int>,<int>) about 20 items and want to find if a given input falls within the range of key and value of dictionary.

Ex:

dictionary items   =    [100,150],
                                    [200,225],
                                    [500,599] etc

If user enters 150, since the 120 exist within the first item range ,I should return true and exit the loop
If user enters 160, since the 160 does not exist on  any dictionary item' s range ,I should return false and exit the loop
If user enters 510 since the 510 exist within the third item range ,I should return true and exit the loop

I want to accomplish above task using c#
0
Comment
Question by:ksd123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40528192
Why do you need a dictionary?  (This is making things rather difficult for you.  A dictionary is really good for rapid lookup of a random item.)
0
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40528249
One way this could be accomplished using a dictionary:
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q28589756
{
	class Program
	{
		private static Dictionary<int, int> items = new Dictionary<int, int>() { {100, 150 }, { 200, 225 }, {500, 599} };
		static void Main(string[] args)
		{
			int getThis = 120;
			Console.WriteLine("Does {0} exist in the dictionary: {1}", getThis, items.Exists(getThis));
			getThis = 160;
			Console.WriteLine("Does {0} exist in the dictionary: {1}", getThis, items.Exists(getThis));
			getThis = 510;
			Console.WriteLine("Does {0} exist in the dictionary: {1}", getThis, items.Exists(getThis));
			Console.ReadLine();
		}
	}

	static class Extensions
	{
		public static bool Exists(this Dictionary<int, int> source, int value)
		{
			return (from KeyValuePair<int, int> item in source where item.Key <= value && item.Value >= value select item).Count() != 0;
		}
	}
}

Open in new window

Produces the following output -Capture.JPGBut don't let this keep you from heeding Andy's point.

-saige-
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40528265
Hi ksd123;

Maybe you could use a collection of Tuple object as shown in the following code snippet.

// In place of using a Dictionary object to hold the data use a 
// list of tuple's and define it like this.
List<Tuple<int, int>> items = new List<Tuple<int, int>>();
// Populate the data as Tuple objects
items.Add(Tuple.Create( 100, 150 ));
items.Add(Tuple.Create( 200, 225 ));
items.Add(Tuple.Create( 500, 599 ));

// Test Data from the user
int userInput = 160;

// The variable result will be true or false after going through the collection
bool result = items.Any( i => userInput >= i.Item1 && userInput <= i.Item2 );

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question