c# - Linq - Dynamic Condition -


i have following query :-

i want add 1 more condition dynamic, if user passes dateofbirth should e.dateofbirth <= date.

var data = ctx.employee.where(e => e.id == id                                 && e.category == category                                 && e.dateofjoining <= date)                                 .select(e => e)                                 .tolist(); 

how condition dynamically?

you can use reflection solve problem there idea may helps you:

  var criteria = new dictionary<string, func<employee, bool>>();    var date = datetime.now;  //or other value    //initialize criterias      criteria.add("dateofbirth", e => e.dateofbirth <= date);   criteria.add("dateofjoining", e => e.dateofjoining <= date);    var selectedvalue = "dateofbirth";    var data = ctx.employee.where(e => e.id == id &&                                      e.category == category &&                                      criteria[selectedvalue](e)).tolist(); 

so if change selectedvalue output based on corresponding criteria looking for.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -