Solved

Looping in Dictionary using c#

Posted on 2015-01-02
3
134 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 32

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now