Skip to main content

Posts

Showing posts from September, 2016

Order by with dynamic columns in LINQ c#

Hi Guys, sometimes we need to sort the data dynamically with dynamic column, suppose we are sowing data in grid and after click on column data should be sort, on first click it should be descending and on again click it should be ascending etc. We know that there are no any technique provide by Microsoft so we can resolve this problem to make an Extension Method as follow.
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks;
namespace DynamicColumnOrderByLinq { publicstaticclassExtMethod     { publicstaticIQueryable<TEntity> CustumOrderBy<TEntity>(thisIQueryable<TEntity> source, string ColName, bool desc)         { if (!string.IsNullOrEmpty(ColName))             { string command = desc ? "OrderByDescending" : "OrderBy"