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

  • Home
  • SEARCH
  • 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 834347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:39:30+00:00 2026-05-15T04:39:30+00:00

The following code: // select all orders var orders = from o in FoodOrders

  • 0

The following code:

// select all orders
var orders = from o in FoodOrders
             where o.STATUS = 1
             order by o.ORDER_DATE descending
             select o;

// if customer id is specified, only select orders from specific customer
if (customerID!=null)
{
    orders = orders.Where(o => customerID.Equals(o.CUSTOMER_ID));
}

gives me the following error:

Cannot implicitly convert type ‘System.Linq.IQueryable’ to ‘System.Linq.IOrderedQueryable’. An explicit conversion exists (are you missing a cast?)

I fixed the error by doing the sorting at the end:

// select all orders
var orders = from o in FoodOrders
             where o.STATUS = 1
             select o;

// if customer id is specified, only select orders from specific customer
if (customerID!=null)
{
    orders = orders.Where(o => customerID.Equals(o.CUSTOMER_ID));
}

// I'm forced to do the ordering here
orders = orders.OrderBy(o => o.ORDER_DATE).Reverse();

But I’m wondering why is this limitation in place? What’s the reason the API was designed in such a way that you can’t add a where constraint after using an order by operator?

  • 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. Editorial Team
    Editorial Team
    2026-05-15T04:39:31+00:00Added an answer on May 15, 2026 at 4:39 am

    The return type of OrderBy is IOrderedQueryable<T>, so that’s the type of the orders variable (partly because you have a no-op projection at the end) – but the return type of Where is just IQueryable<T>. Basically you’ve got a mixture of a no-op projection and an implicitly typed local variable and the last active part of the query is an ordering, and you’re then wanting to reassign the variable. It’s an unhappy combination, basically.

    You could fix it like this:

    IQuerable<FoodOrders> orders = from o in FoodOrders
                                   where o.STATUS == 1
                                   order by o.ORDER_DATE descending
                                   select o;
    
    // if customer id is specified, only select orders from specific customer
    if (customerID!=null)
    {
        orders = orders.Where(o => customerID.Equals(o.CUSTOMER_ID));
    }
    

    Alternatively, if you performed the no-op projection explicitly using dot notation (I suspect the SQL translator will be smart enough to cope!) the type inference would be okay:

    var orders = FoodOrders.Where(o => o.STATUS == 1)
                           .OrderByDescending(o => o.ORDER_DATE)
                           .Select(o => o);
    
    // if customer id is specified, only select orders from specific customer
    if (customerID!=null)
    {
        orders = orders.Where(o => customerID.Equals(o.CUSTOMER_ID));
    }
    

    Or as a final and slightly odd suggestion, you could just change the order of your initial where and orderby clauses. This would be a bad idea in LINQ to Objects, but shouldn’t make a difference in LINQ to SQL:

    var orders = from o in FoodOrders
                 order by o.ORDER_DATE descending
                 where o.STATUS == 1
                 select o;
    
    // if customer id is specified, only select orders from specific customer
    if (customerID!=null)
    {
        orders = orders.Where(o => customerID.Equals(o.CUSTOMER_ID));
    }
    

    Now, in terms of the “why” of the API design: OrderBy and OrderByDescending return IOrderedQueryable so that you can then chain it with ThenBy and ThenByDescending which rely on there being an existing ordering that they can become secondary to, if you see what I mean.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 501k
  • Answers 501k
  • 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
  • Editorial Team
    Editorial Team added an answer You should not depend on volatile to guarantee thread safety,… May 16, 2026 at 2:19 pm
  • Editorial Team
    Editorial Team added an answer The choice shouldn't really be about performance hit but about… May 16, 2026 at 2:19 pm
  • Editorial Team
    Editorial Team added an answer greensound.equals("gsone") May 16, 2026 at 2:19 pm

Trending Tags

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

Top Members

Related Questions

In the following code, how do I select all the <tr> elements that have
I have the following entity code, which returns all users and includes all of
All, I've got the following code, and looking into ways to improve its readibility
I'm using the following snippet to create a select/deselect all checkboxes: http://www.wiseguysonly.com/2010/01/15/select-and-unselect-all-checkboxes-with-jquery/ This is
I'm trying the following code to execute a search and it's not working. On
I have the following code (I know that this code is not optimized but
I have the following code to validate some fields on a sharepoint newform.aspx. To
Evening all I have the following scenarion. I have a range of drop down
I have the following code that should, when run, update a table of victims
Is there a way to select all CSS rules of an element with JavaScript?

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.