Poniższy kod:LINQ to SQL - Dlaczego nie możesz użyć WHERE po ORDER BY?
// 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));
}
daje mi następujący błąd:
Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IOrderedQueryable'. An explicit conversion exists (are you missing a cast?)
Naprawiłem błąd wykonując sortowania na końcu:
// 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();
Ale zastanawiam dlaczego to ograniczenie obowiązuje? Z jakiego powodu API został zaprojektowany w taki sposób, że nie można dodać ograniczenia where
po użyciu operatora order by
?
Co to jest kwerenda no-op? – MCS
To jest naprawdę dokładna odpowiedź i świetne wyjaśnienie. Dzięki. – MCS
@MCS: Miałem na myśli projekcję bez opozycji - klauzulę wyboru, która po prostu wybiera to, co już jest, jeśli widzisz, co mam na myśli. –