Solved

Looping in Dictionary using c#

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate Unique ID in VB.NET 21 105
Data organization issue 7 39
YouTube API get the Video Tags - vb.net 7 33
Inserting LocalDB Table to SQL Server C# 3 22
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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