Is there an efficient way to make a select statement with two variables?

In my C # code, I need to evaluate two non-zero variables. I developed an if-else if statement set, but in my opinion it looks ugly and a little too messy, even if it is correct.

I looked at the MSDN Library and saw only examples for selection based on a single variable.

Is there a cleaner and more compact way to achieve the same result?

Update: I filled out the code to provide more context. Looking at this more, perhaps I can manipulate the linq query directly based on the parameters. However, the question I ask is a general one that I would like to draw attention to: the choice, not the code used after the selection.

public ActionResult Index(string searchBy, string orderBy, string orderDir) { var query = fca.GetResultsByFilter(searchBy); if (orderBy == "Campus" && orderDir == "Asc") { query = query = query.OrderBy(s => s.Campus).ThenBy(s => s.Student_Name); } else if (orderBy == "Campus" && orderDir == "Desc") { query = query.OrderByDescending(s => s.Campus); } else if (orderBy == "Student Name" && orderDir == "Asc") { query = query = query.OrderBy(s => s.Student_Name); } else if (orderBy == "Student Name" && orderDir == "Desc") { query = query.OrderByDescending(s => s.Student_Name); } else if (orderBy == "Course Count" && orderDir == "Asc") { query = query.OrderBy(s => s.Course_Count); } else if (orderBy == "Course Count" && orderDir == "Desc") { query = query.OrderByDescending(s => s.Course_Count); } } 
+5
source share
4 answers

You can create an extension method on IQueryable that processes ordering using OrderByDescending or OrderByDescending :

 public static class QueryableExtensions { public static IOrderedQueryable<TSource> OrderByWithDirection<TSource,TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, string orderDir) { return orderDir == "Desc" ? source.OrderByDescending(keySelector) : source.OrderBy(keySelector); } } 

I assume your GetResultsByFilter method returns an IQueryable<> . If it does return an IEnumerable<> , then the extension method should accept the IEnumerable<TSource> source parameter and instead return an IOrderedEnumerable<TSource> .

This can then be used as follows:

 public ActionResult Index(string searchBy, string orderBy, string orderDir) { var query = fca.GetResultsByFilter(searchBy); switch (orderBy) { case "Campus": query = query.OrderByWithDirection(s => s.Campus, orderDir); break; case "Student Name": query = query.OrderByWithDirection(s => s.Student_Name, orderDir); break; case "Course Count": query = query.OrderByWithDirection(s => s.Course_Count, orderDir); break; } if (orderBy == "Campus" && orderDir == "Asc") { // The Campus Asc case was also ordered by Student_Name in the question. query = query.ThenBy(s => s.Student_Name); } } 
+4
source

CNot sure it’s better, just different.

 switch (orderDir) { case "Asc": Switch (orderBy) { case "Campus": //Code here for Campus orderBy and Asc orderDir break; case "Student Name": //Code here for Student Name orderBy and Asc orderDir break; case "Course Count": //Code here for Course Count orderBy and Asc orderDir break; } break; case "Desc": Switch (orderBy) { case "Campus": //Code here for Campus orderBy and Desc orderDir break; case "Student Name": //Code here for Student Name orderBy and Desc orderDir break; case "Course Count": //Code here for Course Count orderBy and Desc orderDir break; } break; } 
+2
source

I would use the terniary operator to make it more compact and easier to read as follows.

It also cuts out some logical checks, since it does not duplicate them.

  public ActionResult Index(string searchBy, string orderBy, string orderDir) { var query = fca.GetResultsByFilter(searchBy); if (orderBy == "Campus") { query = (orderDir == "Asc") ? query.OrderBy(s => s.Campus).ThenBy(s => s.Student_Name) : query.OrderByDescending(s => s.Campus); } else if (orderBy == "Student Name") { query = (orderDir == "Asc") ? query.OrderBy(s => s.Student_Name) : query.OrderByDescending(s => s.Student_Name); } else if (orderBy == "Course Count") { query = (orderDir == "Asc") ? query.OrderBy(s => s.Student_Name) : query.OrderByDescending(s => s.Course_Count); } } 
+1
source

My welcome:

 public interface IOrder { void perform(Query query) } public abstract class AbstractOrder : IOrder { protected string orderString; public AbstractOrder(string orderString) { this.orderString = orderString; } } public class OrderAsc { public OrderAsc(string orderString) : base(orderString) { } public Query perform(Query query) { query = query.OrderBy(s => s.Course_Count); //here you still have to do a mapping between orderString and your db field s.Course_count return query; } } public class OrderDesc { public OrderDesc(string orderString) : base(orderString) { } public Query perform(Query query) { query = query.OrderByDescending(s => s.Course_Count); //here you still have to do a mapping between orderString and your db field s.Course_count, or maybe it equal, then you can just replace it. return query; } } 

then...

 IList<IOrder> list = new List<IOrder>() {new OrderAsc("Campus"), new OrderDesc("Student Name")} foreach(IOrder o in list) { query = o.perform(query); } 

It may have errors, I do not have an IDE at hand.

0
source

All Articles