Solved

Looping in Dictionary using c#

Posted on 2015-01-02
3
137 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
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 33

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 62

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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