I可查询和排序
似乎在收到 IQueryable 后,我无法修改查询以启用排序。这是代码片段。 我的印象是,使用 IQueryable(而不是 IEnumerable)我应该能够做我正在尝试的事情。
using (oc1 = new NWEntities())
{
string customerid = "ALFKI";
var q = GetOrders((o) => o.CustomerID == customerid);
q.OrderBy(o => o.ShipCity); // DOES NOT WORK !!!!!!!!
foreach (var x in q)
{
Console.WriteLine(string.Format("Order#:{0} City:{1} for Customer {2}",
x.OrderID,
x.ShipCity,
customerid));
}
}
// Returns IQueryable
IQueryable<Order> GetOrders(Expression<Func<Order, Boolean>> predicate)
{
return (oc1.Orders.Where(predicate));
}
输出未按城市排序。我错过了一些明显的东西吗? 感谢您抽出时间。
It seems like that after receiving an IQueryable, I am not able to modify the query to enable a sort. Here is the code snippet.
I am under the impression that with IQueryable (as opposed to do IEnumerable) I should be able to do what I am attempting.
using (oc1 = new NWEntities())
{
string customerid = "ALFKI";
var q = GetOrders((o) => o.CustomerID == customerid);
q.OrderBy(o => o.ShipCity); // DOES NOT WORK !!!!!!!!
foreach (var x in q)
{
Console.WriteLine(string.Format("Order#:{0} City:{1} for Customer {2}",
x.OrderID,
x.ShipCity,
customerid));
}
}
// Returns IQueryable
IQueryable<Order> GetOrders(Expression<Func<Order, Boolean>> predicate)
{
return (oc1.Orders.Where(predicate));
}
The output is somehow not sorted by City. Am I missing something obvious ?
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
OrderBy
的结果存储在另一个变量中。您对列表进行排序,然后默默地删除结果,并对原始结果进行迭代:You need to store the result of
OrderBy
in another variable. You sorted the list, and then silently dropped the result, and did your iteration on the original: