Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 77263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:48:10+00:00 2026-05-10T20:48:10+00:00

I’m using T4 for generating repositories for LINQ to Entities entities. The repository contains

  • 0

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 documentation for Supported and Unsupported Methods does not mention it, but you can’t ‘call’ Skip on a unordered IQueryable. It will raise the following exception:

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’..

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 OrderBy.

I’ve reduced the problem to as less code as possible:

public partial class Repository {     partial void ProvideDefaultSorting(ref IQueryable<Category> currentQuery);      public IQueryable<Category> List(int startIndex, int count)     {         IQueryable<Category> query = List();         ProvideDefaultSorting(ref query);         if (!IsSorted(query))         {             query = query.OrderBy(c => c.CategoryID);         }         return query.Skip(startIndex).Take(count);     }     public IQueryable<Category> List(string sortExpression, int startIndex, int count)     {            return List(sortExpression).Skip(startIndex).Take(count);     }     public IQueryable<Category> List(string sortExpression)     {         return AddSortingToTheExpressionTree(List(), sortExpression);     }     public IQueryable<Category> List()     {            NorthwindEntities ent = new NorthwindEntities();            return ent.Categories;     }      private Boolean IsSorted(IQueryable<Category> query)     {         return query is IOrderedQueryable<Category>;      } }  public partial class Repository {     partial void ProvideDefaultSorting(ref IQueryable<Category> currentQuery)     {         currentQuery = currentQuery.Where(c => c.CategoryName.Contains(' ')); // no sorting..     } } 

This is not my real implementation!

But my question is, how could I implement the IsSorted method? The problem is that LINQ to Entities query’s are always of the type ObjectQuery, which implements IOrderedQueryable.

So how should I make sure an OrderBy method is present in the expression tree? Is the only option to parse the tree?

Update
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 ProvideDefaultSorting partial method has indeed added an OrderBy to the expression tree.

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 ProvideDefaultSorting is not implemented correctly.

So maybe the question is more, how can I confirm that the ProvideDefaultSorting did indeed add sorting to the expression tree.

Update 2
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:48:11+00:00Added an answer on May 10, 2026 at 8:48 pm

    You can address this in the return type of ProvideDefaultSorting. This code does not build:

        public IOrderedQueryable<int> GetOrderedQueryable()     {         IQueryable<int> myInts = new List<int>() { 3, 4, 1, 2 }.AsQueryable<int>();         return myInts.Where(i => i == 2);     } 

    This code builds, but is insidious and the coder gets what they deserve.

        public IOrderedQueryable<int> GetOrderedQueryable()     {         IQueryable<int> myInts = new List<int>() { 3, 4, 1, 2 }.AsQueryable<int>();         return myInts.Where(i => i == 2) as IOrderedQueryable<int>;     } 

    Same story with ref (this does not build):

        public void GetOrderedQueryable(ref IOrderedQueryable<int> query)     {         query = query.Where(i => i == 2);     } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Sure, it's possible in theory. But if your filesystem is… May 11, 2026 at 2:18 pm
  • added an answer Try adding border-collapse: collapse; to your table's styles. Documentation Here.… May 11, 2026 at 2:18 pm
  • added an answer The Duration property is not expressible in a SQL statement… May 11, 2026 at 2:18 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.