C# use LINQ syntax to loop through a Dictionary

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

LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
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-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.