- Where sortColumn is the name of column (string) want to sort.
 sortColumnDir is asc or desc oreder.
var sortColumn = Request.Form.GetValues("columns[" + Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault(); var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault(); var modal = _repo.GetAllResturents(); var RestaurantData = (from tempcustomer in modal select tempcustomer); //Sorting //RestaurantData = RestaurantData.OrderBy(sortColumn); if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir))) { RestaurantData = RestaurantData.OrderBy(sortColumn + " " + sortColumnDir); }i am getting Argument null exception. Note : RestaurantData = RestaurantData.OrderBy(s=>s.Name); is working as expected but does not serve my purpose here. i want to sort on the basic of sortColumn (column name). Please Suggest some better approach for this scenario.
            Asked
            
        
        
            Active
            
        
            Viewed 121 times
        
    1
            
            
        
        Ehsan Sajjad
        
- 61,834
 - 16
 - 105
 - 160
 
        Shani Bhati
        
- 171
 - 1
 - 13
 
- 
                    2`RestaurantData.OrderBy(sortColumn + " " + sortColumnDir)` is not a proper way to perform sort ordering. Consider using `Select` then using `OrderBy` and `ThenBy`. – Tetsuya Yamamoto Jan 15 '19 at 08:17
 - 
                    1If statement will hit if sortColumnDir is null or empty. – MaticDiba Jan 15 '19 at 08:18
 - 
                    1Also worth to read: https://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet-iqueryablet. What you want to use actually called "dynamic LINQ", which requires `System.Linq.Dynamic` namespace. – Tetsuya Yamamoto Jan 15 '19 at 08:30
 - 
                    @TetsuyaYamamoto it is working fine using System.Linq.Dynamic but why it is not a proper way to perform sort ordering. as i am using Linq so writing Select make any performance benefits? – Shani Bhati Jan 15 '19 at 08:55
 - 
                    By using standard `OrderBy` I could tell that's not proper way because `System.Linq.OrderBy()` extension method requires lambda expression rather than string concatenation. The `System.Linq.Dynamic.OrderBy()` accepts string as parameter, hence just use proper library before using extension methods. – Tetsuya Yamamoto Jan 15 '19 at 09:00
 - 
                    The error is rather clear. If you *debug* this, you'll probably see that one of the column names is `null`. Your check returns true only if *both* strings are null. `OrderBy` doesn't work with constant values like the one you passed anyway. It expects a *lambda* that is used to generate the order key – Panagiotis Kanavos Jan 15 '19 at 13:36