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>?
spazjr01Asked:
Who is Participating?
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:
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 -Capture.JPG-saige-
Jacques Bourgeois (James Burger)PresidentCommented:
			List<KeyValuePair<string, Object>> x = null;
			List<Object> y =null;

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

Open in new window

it_saigeDeveloperCommented:
James you have to initialize your list's before you can enumerate them and/or add to them, but you are correct, this is another method:
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 y = new List<SomeObject>();
			foreach (var item in items)
				y.Add(item.Value);
			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

As well as this:
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 y = new List<SomeObject>(from x in items select x.Value);
			foreach (var item in y)
				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

Or this:
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 y = new List<SomeObject>();
			for (int i = 0; i < items.Count - 1; i++)
				y.Add(items[i].Value);

			foreach (var item in y)
				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

Which will all produce the same output as above.

Suffice it to say, there are many different ways to do this.

-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
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
I would just import the LINQ namespace and call ToList:

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

Open in new window

it_saigeDeveloperCommented:
@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-
Jacques Bourgeois (James Burger)PresidentCommented:
@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.
it_saigeDeveloperCommented:
@James,

Understood. ;)

-saige-
spazjr01Author Commented:
Thanks so much.  Very helpful.
kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
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

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.