Sounds a little complex but here is what I am trying to do:
I am filtering certain properties of MyObject
in List<MyObject>
into a new LINQ object as:
var filteredMyObjects = from r in myObjects select new { r.ComponentName, r.Group, r.Key };
The problem is now that Properties ComponentName
, Group
and Key
should come as an input (for example List<string>
of property names). This is used in my logic for exporting data into excel.
I have been trying to combine it with this idea:
typeof(MyObject).GetProperty(property).GetValue(objectInstance) as string
But cannot wrap my head around how to implement it.
EDIT:
See example of what I need to achieve:
List<string> userDefinedPropeties = new List<string> {Property1, Property2, Property3 ... }
var filteredMyObjects = from r in myObjects select new { r.Property1, r.Property2, r.Property3 ... };
Ideal answer would look like this, except this solution does not work in my case: Linq access property by variable