Link to home
Start Free TrialLog in
Avatar of spazjr01
spazjr01Flag for United States of America

asked on

How do I convert a List<KeyValuePair<string, someObject>> to List<someObject>?

In C# .NET, I have a List<KeyValuePair<string, someObject>>.  What is the C# code for converting this to a list of the objects identified in the value of the KeyValuePair?  In other words, how do I convert this to List<someObject>?
Avatar of it_saige
it_saige
Flag of United States of America image

Use the ToDictionary method and get the Values list; e.g. -
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q28932973
{
	class Program
	{
		static List<KeyValuePair<string, SomeObject>> items = new List<KeyValuePair<string, SomeObject>>(from i in Enumerable.Range(0, 5) select new KeyValuePair<string, SomeObject>(string.Format("SomeObject{0}", i), new SomeObject() { Name = string.Format("SomeObject{0}", i), IsEating = i % 2 == 0, Birthday = DateTime.Now.AddMonths(-i) }));

		static void Main(string[] args)
		{
			var listofObjects = items.ToDictionary(x => x.Key, x => x.Value).Values.ToList();
			foreach (var item in listofObjects)
				Console.WriteLine(item);
			Console.ReadLine();
		}
	}

	class SomeObject
	{
		public string Name { get; set; }
		public bool IsEating { get; set; }
		public DateTime Birthday { get; set; }

		public override string ToString()
		{
			return string.Format("{{ Name: {0}; IsEating : {1}; Birthday: {2} }}", Name, IsEating, Birthday.ToShortDateString());
		}
	}
}

Open in new window

Produces the following output -User generated image-saige-
			List<KeyValuePair<string, Object>> x = null;
			List<Object> y =null;

			foreach (object obj in x)
			{
				y.Add(obj);
			}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would just import the LINQ namespace and call ToList:

List<someObject> result = yourDict.Values.ToList();

Open in new window

@Kaufmed, that would work if the author was dealing with a Dictionary<string, someObject>, but the author is dealing with a List<KeyValuePair<string, someObject>>.  Which is why I made my initial recommendation of using the Enumerable.ToDictionary() method.

-saige-
@it_saige.

I did not want to give more than necessary, but oops. Thanks for pointing my mistake. I know that I need to itnitialize the list. I just typed the thing in the editor to test my syntax and forgot to remove the declarations. I am sure that spazjr understood that. He would not ask that question if he did not already have a list to work with.
@James,

Understood. ;)

-saige-
Avatar of spazjr01

ASKER

Thanks so much.  Very helpful.
I don't know why I read that as being a Dictionary; nonetheless, I agree. However, that is still easily remedied with LINQ:

List<object> result = yourDict.Select(kvp => kvp.Value)
                              .ToList();

Open in new window