I have a sample query class which contains query parameters as its’ properties. this class is used for WCF communication and on the server-side appropriate filter expression is generated.
where is sample code.
internal interface IExpressionBuilder<T>
{
Func<T, bool> Build();
}
here is sample implementation
[DataContract]
public class PersonQuery : IExpressionBuilder<Person>
{
[DataMember]
public string IdCardNumber;
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public DateTime? BirthDate { get; set; }
#region Implementation of IExpressionBuilder<Data>
public Func<Person, bool> Build()
{
throw new NotImplementedException();
}
#endregion
}
usage:
publi List<Person> GetPersonByQuery(PersonQuery query)
{
(using context = new SampleContext())
{
return List<Person> foundPersons = context.people.where(query.build());
}
}
u can see that idea is very sample but my question is how to dynamically include PersonQuery properties in expression if they are set, when build() method is called, based on
Have you looked at Dynamic Linq? It seems ideally suited to your problem.
If you want to use functions and build expression trees, this library provides a nice, fluent syntax for creating expression trees.