I wasn't able to find much about how to do this. I'm probably not getting the terminology right.
I have a list of objects:
class Cat()
{
public string Name { get; set; }
}
List<Cat> cats = new List<Cat>();
cats.add(new Cat() { Name = "Felix" } );
cats.add(new Cat() { Name = "Fluffy" } );
How do I get a list of strings from the Name property so it looks like this:
{ "Felix", "Fluffy" }
The LINQ Select operator is your friend:
cats.Select(c => c.Name).ToList()
I am using ToList()
to avoid lazy evaluation and to ensure you have an IList
to work with.
var names = cats.Select(c => c.Name);
But if you still need a list use
List<string> names = cats.ConvertAll(c => c.Name);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With