views:

204

answers:

2

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?

+7  A: 

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.

Jon Skeet
What's a no-op query?
MCS
This is a really thorough answer and a great explanation. Thanks.
MCS
@MCS: I meant a no-op projection - a select clause which just selects what's already there, if you see what I mean.
Jon Skeet
Ok - now I got it. If the select operator wouldn't be no-op - for example, if I had used select o.ORDER_ID instead of select o - the orders var would be converted back to an IQueryable for me and I wouldn't have any type conversion issues.
MCS
I was going to look up what you meant with degenerate query, but you seem to have gotten rid of that term while I was on my way home :)
Thorarin
@Thorarin: A *denegerate query* is one of the form `from x in y select x` - in that case a `Select` call *is* still emitted, whereas `from x in y where x.Foo == 1 select x` will only emit a `Where` call. I used it incorrectly in this post.
Jon Skeet
+4  A: 

It's important to note that var is strongly typed and gets interpretted at compile time. Your first code snippet it essentially the same as:

IOrderedQueryable<FoodOrders> orders = from o in FoodOrders 
         where o.STATUS = 1 
         order by o.ORDER_DATE descending 
         select o; 

When you write the code this way it becomes obvious why you can't assign an IQueryable to a variable declared as IOrderedQueryable.

You can't think of var in the same way you would object

object aString = "Testing...";
var bString = "Testing...";

aString = 1; // Works
bString = 1; // Fails because 1 is not a string

http://msdn.microsoft.com/en-us/library/bb384061.aspx

Cory Charlton