The Extension Method in coding comes with imperative functions and benefits. The Extension method enables you to make an addition of methods to your existing methods. It eliminates the need to create a new derived type, recompile, or any other kind of modification for the original type.
They’re usually known as the instance methods on the extended type. For all the client codes, which are written in C#, F#, and Visual basic, there is no striking difference between calling an extension method and the method which are defined in a type.
The LINQ is one of the most popular kinds of extension methods that you can use for your dynamic ordering. It becomes quite easy in case the number of preferences is 1. However, when you want to generate a LINQ code that has more orderBy count, then you need to use the method of the dynamic query builder.
If you have the data in a table and you want to spot that data in multiple columns, then you will need to store the data of preferences. Let’s say you want to do it for an employee in an organization. Here is how you do it.
EmpID >EmpName> Email> DOB> Salary> Experience
As you have stored the data referenced in a table, and have selected the admin to order the data first by the name of your employee, then salary, and last at the experience, it names the employee whose name comes first had the highest salary and experience. Thus, the LINQ query here will be much easier in case the sort preferences are fixed. However, for dynamic sorting, it will be tough.
For normal cases, your LINQ query will look similar to the-
var EmpData = DB context. tbl_E
To store the preferences, you will need to create a new tbl_UserPresferences.
Get your preference data from the table:
var preferences = dbcontext.sp_UserSortPreference(table_name,UserID).ToList();
List<string> sortBy = new List<string>();
foreach (var preference in preferences)
{
ECNumberHelper obj = new ECNumberHelper();
sortBy.Add(obj.GetFieldName(preference.Option_Text));
}
Now we have the preference data in our variable sortBy.
To create a helper method, we can use a generic concept to generate all the OrderByquery based on the stored preference. The helper method will look like this:
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string columnName, bool isAscending = true)
{
if (String.IsNullOrEmpty(columnName))
{
return source;
}
ParameterExpression parameter = Expression.Parameter(source.ElementType, “”);
MemberExpression property = Expression.Property(parameter, columnName);
LambdaExpression lambda = Expression.Lambda(property, parameter);
string methodName = isAscending ? “OrderBy” : “OrderByDescending”;
Expression methodCallExpression = Expression.Call(typeof(Queryable), methodName,
new Type[] { source.ElementType, property.Type },
source.Expression, Expression.Quote(lambda));
return source.Provider.CreateQuery<T>(methodCallExpression);
}
As we have our query ready, we can pass it and generate our OrderBy query. We have to bridge the added preferences inside the sortBy variable with the query.
For each (var val in sortBy)
{
empData = Helper.OrderBy(empData, val, false);
}
var resultList = empData?.ToList();
return View(resultList);
The extension method used for your dynamic ordering can be a bit tricky, but it can be done easily when you follow up the right code. As you have your data based on your sort preference. Make sure you are passing the data from the Controller to View and then displaying it in a grid.
If you are considering developing a functional web or mobile application for your business, feel free to reach out to us. Polyxer Systems is one of the trusted technology companies in India. We empower wide businesses with Turnkey Software Development.