I have a criteria object in which I was to turn each property into a func, if it's value isn't null.
public class TestClassCriteria
{
public bool? ColumnA { get; set; }
public bool? ColumnB { get; set; }
}
This is what I have thus far, but I am pretty sure I am not defining the lambda correct. This is what I trying to achieve. funcs.Add(x => x.ColumnA == criteria.ColumnA)
.
var properties = criteria.GetType().GetProperties();
var funcs = new List<Func<dynamic, bool>>();
foreach (var property in properties)
{
var propertyName = property.Name;
funcs.Add(x => x.GetType().GetProperty(propertyName).Name == criteria.GetType().GetProperty(propertyName).Name);
}
It's not crashing or causing any error, it just isn't working.
Any help you can provide would be greatly appreciated.
Your current expression is comparing the property names, but I think you want to be comparing the property values:
However, are you sure you need to do this? There would probably be a better way to refactor your code so that you don't need to use reflection to compare properties that happen to have the same name on two unrelated objects.
Do you want something like this?
Sample entity and sample criteria:
Usage:
Instead of your code with dynamics, this code uses strongly typed member access, so, you could cache list of criteria for each pair "Entity - Criteria" and test instances form matching faster.