通过 Linq-to-SQL 提高查询性能以对记录进行排序

发布于 2024-11-19 23:24:02 字数 825 浏览 6 评论 0原文

我正在使用查询从名为customers 的表中获取记录,并且我想按地址、门牌号、姓氏、姓名 对记录进行排序。

首先我使用这个(DataTable)

public CustomerInfo.customers GetCustomers(string zipcode) {
     string sql = "select id, name, surname, zipcode, housenumber where zipcode = @_zipcode order by address, housenumber, surname, name";
 ....     
}

现在我使用这个:

public OrderedEnumerableRowCollection<CustomerInfo.customerRow> GetCustomers(string zipcode) {
     string sql = "select id, name, surname, zipcode, housenumber where zipcode = @_zipcode";

     ....

     return (from c in datatable).OrderBy(c => c.Address).ThenBy(....).ThenBy(...);       
}

这是提高性能的正确方法......?

OrderedEnumerableRowCollectionDataTable 的优缺点是什么?

请让我知道您应该如何执行此操作。

I'm using a query to get records from a table named customers and I want to order the records by address, housenumber, surname, name.

First I used this (DataTable)

public CustomerInfo.customers GetCustomers(string zipcode) {
     string sql = "select id, name, surname, zipcode, housenumber where zipcode = @_zipcode order by address, housenumber, surname, name";
 ....     
}

now I use this:

public OrderedEnumerableRowCollection<CustomerInfo.customerRow> GetCustomers(string zipcode) {
     string sql = "select id, name, surname, zipcode, housenumber where zipcode = @_zipcode";

     ....

     return (from c in datatable).OrderBy(c => c.Address).ThenBy(....).ThenBy(...);       
}

Is that the right way to improve performance ...?

What are the (dis)advantages of the OrderedEnumerableRowCollection vs DataTable?

Please let me know how you should do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

痴者 2024-11-26 23:24:02

我想说这取决于 SQL Server 上的索引哪个更快。例如,如果 SQL Server 已建立索引,可以快速排序(或者数据非常小),那么数据表就可以了。

我通常期望 SQL 服务器在这方面比 .NET 代码更高效。

但就一般建筑而言。如果排序是一个 UI 问题(从您的问题中不清楚是否是),那么排序客户端更有意义,特别是如果您将在其他地方使用数据表(可能以不同的顺序)。

I would say it depends on the indexing at your SQL Server as to which is faster. For instance if the SQL Server is indexed such that it can do the ordering quickly (or the data is very small) then the data table is fine.

I would generally expect the SQL server to be more efficient at this than the .NET code.

On a general architectual note though. If the ordering is a UI concern (it's not clear from your question if it is) then the ordering client-side makes more sense, especially if you'll use the datatable elsewhere (potentially in a different order).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文