vendredi 4 septembre 2015

Convert List

So I have a list of Objects lets say for example:

var animals = new List<Animal>() //add Animals`

I know I can convert these to strings from This Question like:

List<Animal> animals = new List<Animal>();
List<string> strings = animals.Select(a => (string)a).ToList();

BUT I would like to get List<string> from the list of animals based on a property that I feed in as a string at runtime

For example:

private List<string> GetList(string property)
{
    return animals.Select(x => filterByReflection(property)).ToList();
}

so I can call:

var names = GetList("Name");
var types = GetList("Type");
var ages = GetList("Age");

Is this possible?





Aucun commentaire:

Enregistrer un commentaire