Solved

Looping in Dictionary using c#

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

635 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