Convert Enum to Dictionary for binding

Posted on: Thu Dec 09 11:46:41 -0800 2010. Updated on: Thu Dec 09 11:48:25 -0800 2010.
Category: .NET C#

Sometimes we need to bind an enum to a dropdown or something of the sort. Sometimes we want to join in a linq query. The best way to do this is to convert the Enum to a typed Dictionary

public static class Utilities
	{
		public static Dictionary ConvertToDictionary(Type enumType)
		{
			// get the names and values from the enumeration
			List names = Enum.GetNames(enumType).ToList();
			Array values = Enum.GetValues(enumType);

			Dictionary dict = new Dictionary();

			for (int i = 0; i < names.Count; i++)
				dict.Add((int)values.GetValue(i), names[i]);

			return dict;
		}
	}

Comments:

Mon Jan 03 02:39:33 -0800 2011
If you are in a not good position and have no money to go out from that point, you will need to take the <a href="http://bestfinance-blog.com">loan</a>. Because it will help you definitely. I take car loan every single year and feel OK because of it.


---------------------------------------------------

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.