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

spazjr01
spazjr01 used Ask the Experts™
on
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>?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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-
Top Expert 2015

Commented:
			List<KeyValuePair<string, Object>> x = null;
			List<Object> y =null;

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

Open in new window

Commented:
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-
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
I would just import the LINQ namespace and call ToList:

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

Open in new window

Commented:
@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-
Top Expert 2015

Commented:
@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.

Commented:
@James,

Understood. ;)

-saige-

Author

Commented:
Thanks so much.  Very helpful.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

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

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