C# use LINQ syntax to loop through a Dictionary

Tom Knowlton
Tom Knowlton used Ask the Experts™
on
Speaking as a total LINQ newbie:

Is there a more "LINQ" ish way to interate over list of integers and fill the dictionary?

The commented code works ...  I am just looking for a more LINQ like syntax / solution.

Please provide code in C#....

 public IDictionary<int, string> ReturnIntStringCollection(ICollection<int> IntegerCollection)
        {
            //Dictionary<int, string> DictionaryOfIntsAndStrings = new Dictionary<int, string>();

            //foreach (int tempInt in IntegerCollection)
            //{
            //    NumPlusKeyword npkw = new NumPlusKeyword();
            //    npkw = IntegerIsValid(tempInt);
            //    if (npkw.NumValid)
            //    {
            //        DictionaryOfIntsAndStrings.Add(npkw.Number, npkw.KeyWord);
            //    }
            //}

            //return DictionaryOfIntsAndStrings;
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Just making assumptions based upon your code, here is a potential implementation:
public IDictionary<int, string> GetIntegerStringCollection(ICollection<int> source)
{
	return (from i in source let npkw = IntegerIsValid(i) where npkw.NumValid select new KeyValuePair<int, string>(npkw.Number, npkw.Keyword)).ToDictionary(x => x.Key, x => x.Value);
}

Open in new window

Example usage -
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q28711502
{
	class Program
	{
		static int[] range = (from i in Enumerable.Range(0, 20) select i).ToArray();

		static void Main(string[] args)
		{
			foreach (var item in range.GetIntegerStringCollection())
				Console.WriteLine("Key = {0}; Value = {1}", item.Key, item.Value);
			Console.ReadLine();
		}
	}

	class NumPlusKeyword
	{
		public int Number { get; set; }
		public string Keyword { get; set; }

		public bool NumValid { get { return Number > 0; } }
	}

	static class Extensions
	{
		public static IDictionary<int, string> GetIntegerStringCollection(this ICollection<int> source)
		{
			return (from i in source let npkw = IntegerIsValid(i) where npkw.NumValid select new KeyValuePair<int, string>(npkw.Number, npkw.Keyword)).ToDictionary(x => x.Key, x => x.Value);
		}

		public static NumPlusKeyword IntegerIsValid(this int i)
		{
			int[] range = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
			var match = (from value in range where value.Equals(i) select value).FirstOrDefault();
			return new NumPlusKeyword() { Number = match, Keyword = match.AsString() };
		}

		public static string AsString(this int i)
		{
			if (i.Equals(1))
				return "ONE";
			if (i.Equals(2))
				return "TWO";
			if (i.Equals(3))
				return "THREE";
			if (i.Equals(4))
				return "FOUR";
			if (i.Equals(5))
				return "FIVE";
			if (i.Equals(6))
				return "SIX";
			if (i.Equals(7))
				return "SEVEN";
			if (i.Equals(8))
				return "EIGHT";
			if (i.Equals(9))
				return "NINE";
			if (i.Equals(10))
				return "TEN";
			return string.Empty;
		}
	}
}

Open in new window

Produces the following output -Capture.JPG
-saige-

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial