For sake of brevity, assume I have a model named Participants like
public class Participant()
{
public int? ID {get; set;}
public string Name {get; set;}
public DateTime JoinDate {get; set;}
public string Address1 {get; set;}
public string City {get; set;}
public string County {get; set;}
}
public IList<Participant> SearchParticipants(Participant objParticipant)
{
using (Db.Context())
{
//HOW CAN I ACHEIVE THIS? USING EF
//WARNING PSEUDO-CODE / MAGIC FUNCTION (SearchMatches) BELOW
return Db.Entities<Participant>().SearchMatches(objParticipant);
}
}
Basically, I do not want to construct multiple .where(k => k.PropertyName) queries. I think certain PHP MVC frameworks have this, pass an object with certain properties filled in and one gets a array (in our case IList) of matching results from the DB.
You can do this using a combination of reflection, to loop through your properties, and dynamic linq to build the where predicates.
Below is a simple example as a starting point / proof of concept.
Currently it uses
nullas the value indicating that we don't want a where clause, but if you had a value type property (likeint) then you could add an attribute like[FilterIgnore]or something and check that when you loop through the properties.Make a new console application and replace program.cs with this then add the dynamic linq file from the sample to the project: