mercredi 17 novembre 2021

C# - How to use Select LINQ method using properties from a given strings array

Suppose we have a simple class called Person with properties as FirstName, LastName, Age.

Now I have a list of properties as a list of strings.

var props = new List<string> { "FirstName", "Age"}.

Now I have a List of Person objects.

var persons = new List<Person> {....}

How to use Select LINQ method to get just the Person properties that appear in the props?

var result = persons.Select(p => code here)

The result should be a list of anonymous objects, where a anonymous list's item should contain the properties from props list.





Aucun commentaire:

Enregistrer un commentaire