Các mã sau đây:LINQ to SQL - Tại sao bạn không thể sử dụng WHERE sau khi 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));
}
mang lại cho tôi những lỗi sau:
Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IOrderedQueryable'. An explicit conversion exists (are you missing a cast?)
Tôi cố định các lỗi bằng cách làm như sắp xếp ở cuối:
// 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();
Nhưng tôi tự hỏi tại sao giới hạn này lại được áp dụng? Lý do API được thiết kế sao cho bạn không thể thêm ràng buộc where
sau khi sử dụng toán tử order by
?
một truy vấn không-op là gì? – MCS
Đây là một câu trả lời thực sự toàn diện và là một lời giải thích tuyệt vời. Cảm ơn. – MCS
@MCS: Tôi có nghĩa là một phép chiếu không có - một mệnh đề lựa chọn chỉ chọn những gì đã có, nếu bạn hiểu ý tôi. –