How to check for the presence of an OrderBy in a ObjectQuery expression tree

后端 未结 6 1052
礼貌的吻别
礼貌的吻别 2020-12-09 07:09

I\'m using T4 for generating repositories for LINQ to Entities entities.

The repository contains (amongst other things) a List method suitable for paging. The docum

6条回答
  •  盖世英雄少女心
    2020-12-09 07:39

    I'm afraid it's a bit harder than that. You see, the Entity Framework will, in certain circumstances, silently ignore an OrderBy. So it isn't enough to just look for an OrderBy in the expression tree. The OrderBy has to be in the "right" place, and the definition of the "right" place is an implementation detail of the Entity Framework.

    As you may have guessed by now, I'm in the same place as you are; I'm using the entity repository pattern and doing a Take/Skip on the presentation layer. The solution I have used, which is perhaps not ideal, but good enough for what I'm doing, is to not do any ordering until the last possible moment, to ensure that the OrderBy is always the last thing into the expression tree. So any action which is going to do a Take/Skip (directly or indirectly) inserts an OrderBy first. The code is structured such that this can only happen once.

提交回复
热议问题