public ActionResult Index(string d, string c) { var q = _repo.GetAll(); if (String.IsNullOrEmpty(d) || String.IsNullOrEmpty(c)) { d = "asc"; c = "EmailAddress"; } //TODO: make this dynamic and generic. Look at: //http://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet //http://stackoverflow.com/questions/307512/how-do-i-apply-orderby-on-an-iqueryable-using-a-string-column-name-within-a-gene bool asc = (d == "asc"); switch (c) { case "EmailAddress": q = asc ? q.OrderBy(i => i.EmailAddress) : q.OrderByDescending(i => i.EmailAddress); break; case "Disabled": q = asc ? q.OrderBy(i => i.EmailAddress) : q.OrderByDescending(i => i.EmailAddress); break; } ViewBag.Direction = d; ViewBag.Column = c; return(View(new AdminBaseViewModel <IEnumerable <AdminUser> > { Item = q.ToList() })); }