0

I'm trying to extend/copy this code by marisks

to include a default sorting interceptor. But for the life of me, I cannot wrap my head around the expressions and bindings.

Inspired by the SoftDelete code

var table = (EntityType)expression.Target.ElementType;
            if (table.Properties.All(p => p.Name != IsDeletedColumnName))
            {
                return base.Visit(expression);
            }

            var binding = expression.Bind();
            return binding.Filter(
                 binding.VariableType
                      .Variable(binding.VariableName)
                      .Property(IsDeletedColumnName)
                      .IsNull()
                );

I'm trying to add a similar thing to sort data be default. This is the closest I've come (that will compile)

var table = (EntityType)expression.Target.ElementType;
            string sortingColumn = "Priority";

            var binding = expression.Bind();

            return binding.Filter(
                 binding.VariableType
                      .Variable(binding.VariableName)
                      .Property(sortingColumn)
                      .OrderBy(m => m)
                );

but it throws an exception at runtime:

DbExpressionBinding requires an input expression with a collection ResultType. Parameter name: input

Can someone help me fix this - and possibly help me understand what's going on?

1
  • Can you post somewhere a working VS project that I can use to try and test?
    – Akos Nagy
    Sep 1, 2017 at 12:20

1 Answer 1

1

I did some hacking on my own. Basically the problem is that you try to create a Filter expression, but you need a Sort expression. Something like this:

var sortingColumn = "OrderProp";
var binding = expression.Bind();
return DbExpressionBuilder.Sort(binding,
                new[] {
                    DbExpressionBuilder.ToSortClause(binding.VariableType.Variable(binding.VariableName).Property(sortingColumn) )
                });

Of course, this only works, if you have the "OrderProp" on the type itself (you have to add extra checks for that).

2
  • Sorry for not getting back to you sooner :-/ But thanks for the effort. Your solution is great. It was the whole .ToSortClause syntax I didn't understand. Thanks a bunch for your help! Much appreciated. Sep 3, 2017 at 7:10
  • No problem, glad I could help. The Sort method needs a list of sort clauses (a clause determines a sorting key and a sorting direction). The sorting key can be anything, but in this case it is just an expression in the form of e=>e.OrderProp, which is basically the same as it is in the case of filtering for a boolean deleted flag, i.e. you need a parameter in the same structure.
    – Akos Nagy
    Sep 3, 2017 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.