1

I recently develop a handler for DataTable for MVC, but I am having an issue. For some reason, when I am sorting my data to be retrieved from my MySql server, it gives me duplicates of the same people every time.

I figured if this was just odd that it would be random people or everyone, but this is not the case. I narrowed the problem to this piece of my code, but I have tried several things and I cannot understand why.

I have done a straight forward sort and it worked just fine. I can only conclude that this something to do with my machine because I do not remember having this issue on another one. If anyone can help me out and understand why it is doing this, I would be grateful.

Expression<Func<T, object>> sortExpression = null;
                if(lambda.Lambda == null)
                {
                    var param = Expression.Parameter(typeof(T), "item");
                    sortExpression = Expression.Lambda<Func<T, object>>(
                        Expression.Property(param, lambda.ColumnName), param);
                        //Expression.Convert(Expression.Property(param, lambda.ColumnName), typeof(object)), param);
                }

                if(order == null) { 
                    if(lambda.IsDescending)
                    {
                        if (sortExpression != null)
                            order = results.OrderByDescending(sortExpression.Compile());
                        else
                            order = results.OrderByDescending(lambda.Lambda);
                    }
                    else
                    {
                        if (sortExpression != null)
                            order = results.OrderBy(sortExpression.Compile());
                        else
                            order = results.OrderBy(lambda.Lambda);
                    }
                }
                else
                {
                    if (lambda.IsDescending)
                    {
                        if (sortExpression != null)
                            order = order.ThenByDescending(sortExpression.Compile());
                        else
                            order = order.ThenByDescending(lambda.Lambda);
                    }
                    else
                    {
                        if (sortExpression != null)
                            order = order.ThenBy(sortExpression.Compile());
                        else
                            order = order.ThenBy(lambda.Lambda);
                    }
                }
            }
Mrinal Kamboj
  • 10,673
  • 4
  • 29
  • 58
Matthew
  • 2,887
  • 3
  • 14
  • 27
  • To start with this is not the correct way of using Expression Trees, you are still creating a Func delegate, using if else loop. ideally a Func shall be generated by Expression Tree on Compilation and that should contain multiple OrderBy / Then By statements. If are doing this, `results.OrderBy(sortExpression.Compile())`, you can as well use delegate directly – Mrinal Kamboj May 12 '19 at 07:59
  • What would be the correct way of using expression trees for my need for this? I recently read into expressions due to this snippet. For the delegation, wouldn't that require the programmer for each and every new instance of this to create a new delegate for this to work? My goal is to create this to be autonomous and generic for any DataTable to work with server-side processing. The developers aren't, well shouldn't, be passing me any data to my class and they just get their response back. – Matthew May 12 '19 at 08:08
  • Expression trees are suppose to generate Delegate at run-time and provide a different delegate based on the Input modification, here you are creating delegate using metadata, so expression tree is not required. In fact I am not sure how does Sorting leads to duplicate, since sorting is one of those operation, which doesn't change data, even if you remove the sorting you will still get duplicates and that's where you shall start, since sorting will not change anything in the data. Doing is for `DataTable / List` is just a wrapper – Mrinal Kamboj May 12 '19 at 08:27
  • Check out you need a code like this to make it genuinely work via `Expression Trees`, https://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet-iqueryablet?rq=1, though that would not solve the duplicate data issue – Mrinal Kamboj May 12 '19 at 08:36
  • Another good example, https://stackoverflow.com/questions/11336713/how-do-i-create-an-expression-tree-for-run-time-sorting – Mrinal Kamboj May 12 '19 at 08:41

0 Answers0