Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check for the presence of an OrderBy in a ObjectQuery<T> expression tree
    primarykey
    data
    text
    <p>I'm using T4 for generating repositories for LINQ to Entities entities. </p> <p>The repository contains (amongst other things) a List method suitable for paging. The documentation for <a href="http://msdn.microsoft.com/en-us/library/bb738474.aspx" rel="nofollow noreferrer">Supported and Unsupported Methods</a> does not mention it, but you can't "call" <code>Skip</code> on a unordered <code>IQueryable</code>. It will raise the following exception:</p> <blockquote> <p>System.NotSupportedException: The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'..</p> </blockquote> <p>I solved it by allowing to define a default sorting via a partial method. But I'm having problems checking if the expression tree indeed contains an <code>OrderBy</code>.</p> <p>I've reduced the problem to as less code as possible:</p> <pre><code>public partial class Repository { partial void ProvideDefaultSorting(ref IQueryable&lt;Category&gt; currentQuery); public IQueryable&lt;Category&gt; List(int startIndex, int count) { IQueryable&lt;Category&gt; query = List(); ProvideDefaultSorting(ref query); if (!IsSorted(query)) { query = query.OrderBy(c =&gt; c.CategoryID); } return query.Skip(startIndex).Take(count); } public IQueryable&lt;Category&gt; List(string sortExpression, int startIndex, int count) { return List(sortExpression).Skip(startIndex).Take(count); } public IQueryable&lt;Category&gt; List(string sortExpression) { return AddSortingToTheExpressionTree(List(), sortExpression); } public IQueryable&lt;Category&gt; List() { NorthwindEntities ent = new NorthwindEntities(); return ent.Categories; } private Boolean IsSorted(IQueryable&lt;Category&gt; query) { return query is IOrderedQueryable&lt;Category&gt;; } } public partial class Repository { partial void ProvideDefaultSorting(ref IQueryable&lt;Category&gt; currentQuery) { currentQuery = currentQuery.Where(c =&gt; c.CategoryName.Contains(" ")); // no sorting.. } } </code></pre> <p>This is not my real implementation!</p> <p>But my <strong>question</strong> is, how could I implement the <code>IsSorted</code> method? The problem is that LINQ to Entities query's are always of the type <code>ObjectQuery</code>, which implements <code>IOrderedQueryable</code>.</p> <p>So how should I make sure an <code>OrderBy</code> method is present in the expression tree? Is the only option to parse the tree?</p> <p><strong>Update</strong><br> I've added two other overloads to make clear that it's not about how to add sorting support to the repository, but how to check if the <code>ProvideDefaultSorting</code> partial method has indeed added an <code>OrderBy</code> to the expression tree.</p> <p>The problem is, the first partial class is generate by a template and the implementation of the second part of the partial class is made by a team member at another time. You can compare it with the way the .NET Entity Framework generates the EntityContext, it allows extension points for other developers. So I want to try to make it robust and not crash when the <code>ProvideDefaultSorting</code> is not implemented correctly.</p> <p>So maybe the question is more, how can I confirm that the <code>ProvideDefaultSorting</code> did indeed add sorting to the expression tree.</p> <p><strong>Update 2</strong><br> The new question was answered, and accepted, I think I should change the title to match the question more. Or should I leave the current title because it will lead people with the same problem to this solution?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload