c# - Parameterize database column names in controller action -
i want controller action "professorstatus" queries "professor" table , returns list of active professors particular school.
here controller action have, struggling parameterizing "school" columns
public actionresult professorstatus(string schooltype) { var activeprofessors = (from p in prof.professortable.where(a => a.engineering.value == true) group p p.professorid g select g.key).tolist(); return view(activeprofessors); }
now, in above controller action. instead of hard coding "engineering" want parameterize "schooltype".
so if pass schooltype = medicine controller display professors medicine school , on other school types.
how can avoid hardcording here? in advance.
if not in position redesign whole database , systems use it, can still around using expressions.
public actionresult professorstatus(string schooltype) { expresison<func<professor, bool>> filter; switch (schooltype) { case "engineering": filter= => a.engineering.value == true; break; default: throw new exception("unknown schooltype - " + schooltype); } var activeprofessors = (from p in prof.professortable.where(filter) group p p.professorid g select g.key).tolist(); return view(activeprofessors); }
Comments
Post a Comment