c# - Filtering a list by comparing enums against a selected filter -
i have class named "simplesearchcriteria" 1 of properties enum named "opportunitystatus" containing various statuses. property set depending on users selection, drop down menu.
below relevant code that's held in method, creating list of type "opportunitystatus", adding default values list within if statement , filtering based on value of "simplesearchcriteria" using switch statement
relevant method code
var states = new list<opportunitystatus>(); if(searchcriteria.oppstatusid == null) { states.add(opportunitystatus.active); states.add(opportunitystatus.closed); states.add(opportunitystatus.draft); } else { filterstates(searchcriteria, isadmin, states); }
filterstates method
private static void filterstates(simplesearchcriteria searchcriteria, bool isadmin, list<opportunitystatus> states) { if (isadmin) { switch (searchcriteria.oppstatusid) { case opportunitystatus.active: states.add(opportunitystatus.active); break; case opportunitystatus.draft: states.add(opportunitystatus.draft); break; case opportunitystatus.closed: states.add(opportunitystatus.closed); break; case opportunitystatus.available: states.add(opportunitystatus.active); states.add(opportunitystatus.draft); break; } } }
i should add reason checking null first have been advised it's better treat condition separate case, rather evaluating each scenario in switch statement , placing condition default.
now above works fine, i've become aware of c# dictionaries not sure if practical in scenario? maybe there more practical / preferred approach implementing this, if offer advise?
thanks
Comments
Post a Comment