Hi I would like to create a dynamic search query using entity with values from the URI to be the search fields.
The code below works but some of the tables I have to search on have over 200 fields and I would like to create something that would take the property name and allow me to search on it.
It also can have multiple search options, for example surname and given name and Date of birth
queryable = queryable.Where(x => x.<PROPERTY.NAME>.Contains(query.<PROPERTY.NAME>));
Is this possible?
This is my code so far.
public List<EMPLOYEE> Get([FromUri] EMPLOYEE query)
{
List<EMPLOYEE> emps = new List<EMPLOYEE>();
var db = AuthHandler.Ent;
var queryable = db.EMPLOYEES.AsExpandable();
foreach (var prop in query.GetType().GetProperties())
{
if (prop.GetValue(query, null) != null)
{
switch (prop.Name)
{
case "EMP_CREATIONDATE":
queryable = queryable.Where(x => x.EMP_CREATIONDATE.Equals(query.EMP_CREATIONDATE));
break;
case "EMP_SURNAME":
queryable = queryable.Where(x => x.EMP_SURNAME.Contains(query.EMP_SURNAME));
break;
case "EMP_GIVENNAMES":
queryable = queryable.Where(x => x.EMP_GIVENNAMES.Contains(query.EMP_GIVENNAMES));
break;
}
queryable = queryable.Where(x => x.EMP_SURNAME.Contains(query.EMP_SURNAME));
}
}
emps = queryable.ToList();
return emps;
}