I have a simple class with many DateTime
properties:
public class MyClass
{
public DateTime Created {get; set;}
public DateTime LastChanged {get; set;}
public DateTime LastAccessed {get; set;}
...
}
Later, somewhere in my code I would like to filter collection of MyClass
based on these properties. For each property I would like to do something like this:
myQueryableOfMyClass = myQueryableOfMyClass.Where(a => ((begin == null) || (a.Created >= begin)) && ((end == null) || (a.Created <= end));
If I had to do this for each of my DateTime
property, it would be a lot of code with some risk of a typo, so I would like to do something like this:
myQueryableOfMyClass = Filter(myQueryableOfMyClass, begin, end, MyClass.Created);
myQueryableOfMyClass = Filter(myQueryableOfMyClass, changebegin, changeend, MyClass.LastChanged);
myQueryableOfMyClass = Filter(myQueryableOfMyClass, accbegin, accend, MyClass.LastAccessed);
...
where the Filter
method is implemented using the LINQ Where
as in the first example.
The code above does not compile, of course, because MyClass.Created
is a nonsense.
There must be some solution using reflection, but I have little very experience with reflection. Could you please help me?
Aucun commentaire:
Enregistrer un commentaire