What's the best way to do this where PROPNAME
could be any property of type T
? Build it up with reflection, or is there a good LINQ way of doing this?
T[] vals = people.Select(x => x.PROPNAME).ToArray<T>();
this is the best I've got so far:
public T[] ConvertListCol<TFrom,T>(IEnumerable<TFrom> list, string col)
{
var typ = typeof (TFrom);
var props = typ.GetProperties();
var prop = (from p in props where p.Name == col select p).FirstOrDefault();
var ret = from o in list select prop.GetValue(o, null);
return ret.ToArray<T>();
}
Got an error in the return... but getting closer. It's okay, but a little messier than I'd hoped.
Using a non-explicit var type, it will automatically generate the type for you
var names = people.Select(x => x.PROPNAME).ToArray();
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