c# - Performing Linq operation on multiple possible combination of nullable values -
the title , method explains every thing. need perform search on bases of multiple options. don't want write possible conditions. suggestions?
public list<ticket> gettickets(status? status,priority? priority,ticketlevel? ticketlevel, datetime? createdon,datetime? modifiedon,bool? removed) { if(status!=null && priority !=null && ticketlevel!=null && createdon==null && modifiedon==null && removed ==null) { using (crmcontext context=new crmcontext()) { return context.tickets.where( t => t.status == status && t.priority == priority && t.ticketlevel == ticketlevel).tolist(); } } throw new notimplementedexception(); }
thank you!
you use solution dave bish, or use ternary operator here.
return context.tickets.where(t => (status != null ? t.status == status : true) && (priority != null ? t.priority == priority : true) && (ticketlevel != null ? t.ticketlevel == ticketlevel : true)).tolist();
there obvious advantages either. solution dave provided has advantage of readability/maintenance. downfall is chaining, , therefore can only and (&&)
condition.
the solution here has ability can change &&
operators ||
well. downfall readability/ease of maintenance.
personally, whichever choose you.
Comments
Post a Comment