I可查询和排序

发布于 2024-11-06 21:22:48 字数 820 浏览 0 评论 0原文

似乎在收到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2024-11-13 21:22:48

您需要将 OrderBy 的结果存储在另一个变量中。您对列表进行排序,然后默默地删除结果,并对原始结果进行迭代:

var q = GetOrders(...);

// notice I store in **q2**
var q2 = q.OrderBy(o => o.ShipCity);
foreach (var x in q2)

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:

var q = GetOrders(...);

// notice I store in **q2**
var q2 = q.OrderBy(o => o.ShipCity);
foreach (var x in q2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文