vendredi 22 mars 2019

Dynamic Where Clause in EF Core 2.2

I want to dynamicly add a where clause to my LINQ query. I have the filter property name and the filter property value, so I need to build something like this:

var assignmentListQuery = context.Assignments;

if (!string.IsNullOrWhiteSpace(bookingStep.FilterPropertyName) && !string.IsNullOrWhiteSpace(bookingStep.FilterPropertyValue))
{
    assignmentListQuery = assignmentListQuery.Where(item => PROPERTYNAME == PROPERTYVALUE)
}

ar assignmentList = await assignmentListQuery.ToListAsync();

I've tried to get the propertyinfo of the property, which seems not to me here.

var item = context.Set<Assignment>().First();
object value = item.GetType().GetProperty(bookingStep.FilterPropertyName).GetValue(item, null);

Has anyone an idea on how to create this kind of where clause?





Aucun commentaire:

Enregistrer un commentaire