I have a class and an array of property names, which are defined as follows:
public class Dog {
public string Name { get; set; }
public string Breed { get; set; }
public int Age { get; set; }
}
var desiredProperties = new [] {"Name", "Breed"};
I also have a method that returns a list of dog objects:
List<Dog> dogs = GetAllDogs();
Is there a way to return a subset dogsthat contains only the properties defined in the array desiredProperties? In the end, this resulting list will be serialized for JSON.
I’ve been struggling with this problem for some time, given that the user will be allowed to specify any combination of properties (provided that they are all valid) as a result in the array. Some more examples:
var desiredProperties = new [] {"Name", "Age"};
var desiredProperties = new [] {"Breed", "Age"};
source
share