lundi 3 avril 2023

C# Build anonymous type from array string

I'm looking for a way to build an anonymous type from an object where it's property names match those in a given string array. Something like:

//Current Way:
    var anonObject = primaryObject.Select(m=> new {m.property1, m.property2, m.property3, m.property4, ...etc});
//Ideal Way:
    var propertyArray= new string["property1","property3"];
    var anonObject = primaryObject.Select(m=> new { /* Something here where we step through primaryObject.GetProperties() and only choose the ones where the name is contained in the above array */ });

Any ideas on how to achieve this?





Aucun commentaire:

Enregistrer un commentaire