Question: Say I have a data class like this and a list of its objects:

```
public class DataSet
{
    public int A { get; set; }
    public string B { get; set; }
    public double C { get; set; }
}

var data = new List<DataSet>
{
    new DataSet() { A = 1, B = "One", C = 1.1 },
    new DataSet() { A = 2, B = "Two", C = 2.2 },
    new DataSet() { A = 3, B = "Three", C = 3.3 }
};

```

I would like to do a `Select()` on the list, based on different properties. For example, if I need a list of property `A`, I could do this easily:

```
var listA = data.Select(x => x.A).ToList();

```

All good so far.

But in my program, I need to do the above, only, I wouldn't know whether I need a list of `A` or `B` or `C` until runtime. This 'knowledge' of what to select is stored in a list of strings, and I need to iterate it and extract only the appropriate lists. Something like this:

```
// GetKeys() will return the keys that I need to extract.
// So at one time keyList could have "A" and "B", another time "B" and "C" etc.
List<string> keyList = GetKeys();

foreach (var key in keyList)
{
    // What do I do here?
    data.Select(x =>???).ToList();
}

```

Is this possible at all? I'm fine with even a non-LINQ solution, if it achieves my goal.

---

**EDIT:
Clarifying the requirement.**

The end result I want is a separate list based on each 'key' mentioned above. So, something like

`List<List<object>>`

The `count` in outer list would be the `count` of `keyList`.
The inner list would have as many items as in `DataSet`.

Answer: 
This would probably not be the most efficient solution, but you could use Reflection for a fully dynamic solution:

```
private static List<List<object>> SelectDynamicData<T>(IEnumerable<T> data, List<string> properties)
{
    // get the properties only once per call
    // this isn't fast
    var wantedProperties = typeof(T)
        .GetProperties()
        .Where(x => properties.Contains(x.Name))
        .ToArray();

    var result = new Dictionary<string, List<object>>();

    foreach (var item in data)
    {
        foreach (var wantedProperty in wantedProperties)
        {
            if (!result.ContainsKey(wantedProperty.Name))
            {
                result.Add(wantedProperty.Name, new List<object>());
            }

            result[wantedProperty.Name].Add(wantedProperty.GetValue(item));
        }
    }

    return result.Select(x => x.Value).ToList();
}

```

And, of course, you'd need to do a double foreach or a LINQ query to print that. For example:

```
var data = new List<DataSet>
{
    new DataSet() { A = 1, B = "One", C = 1.1 },
    new DataSet() { A = 2, B = "Two", C = 2.2 },
    new DataSet() { A = 3, B = "Three", C = 3.3 }
};

var selectedData = SelectDynamicData(data, new List<string> { "A", "C" });

foreach (var list in selectedData)
{
    foreach (object item in list)
    {
        Console.Write(item + ", ");
    }
    Console.WriteLine();
}

```
You can use reflection, for example

```
        string key = "A";

        var query = data.Select(x =>
        {
            var prop = x.GetType().GetProperty(key); //NOTE: if key does not exist this will return null
            return prop.GetValue(x);
        });

        foreach (var value in query)
        {
            Console.WriteLine(value); //will print 1, 2, 3
        }

```