Linq-to-SQL Orderby thenby [英] Linq-to-sql orderby thenby

查看:57
本文介绍了Linq-to-SQL Orderby thenby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下查询语法

I'm using the following query syntax

from table
where 
where
orderby 
orderby

第一个orderby是一个日期,第二个orderby是一个日期.我认为这可以像orderby thenby一样工作,但似乎在做其他事情.

Where the first orderby is a date and second orderby is a date. I would assume this would work like orderby thenby but appears to be doing something else.

  1. 如何使用上述语法而不使用扩展语法来进行订购. (明白了)

  1. How can I do an orderby thenby using the above syntax without using extension syntax. (Got it)

orderby,orderby有什么作用?

And what does the orderby, orderby do?

推荐答案

然后如何使用上述语法而不使用扩展语法来进行订购.

How can I do an orderby thenby using the above syntax without using extension syntax.

在字段之间使用逗号:

orderby a, b


orderby,orderby有什么作用?

And what does the orderby, orderby do?

当您连续两次使用orderby时,概念上将首先使用第一个orderby对元素进行排序,然后使用第二个orderby再次对元素进行排序.因为排序定义为稳定排序(与第二个对象相关联的对象orderby将保持与第一个orderby排序后的顺序相同,这实际上意味着:

When you use orderby twice in a row the elements conceptually will first be sorted using the first orderby, and then sorted again using the second orderby. Because the sorting is defined to be a stable sort (objects which are tied with the second orderby will remain in the same order as after sorting with the first orderby it effectively means that this:

var query = from x in l
            orderby x.A
            orderby x.B
            select x;

等效于:

var query = from x in l
            orderby x.B, x.A
            select x;

结果是将orderby术语与您可能的意图互换了.

The result is that the orderby terms are swapped from what you probably intended.

使用LINQ to SQL进行测试

这可以通过在LINQ to SQL中进行尝试来验证.我创建了以下查询:

This can be verified by trying it in LINQ to SQL. I created the following query:

var query = from a in dc.Orders
            orderby a.Date
            orderby a.CustomerID
            select a;

这是生成的SQL:

SELECT [t0].[ID], [t0].[CustomerID], [t0].[Date], [t0].[Description]
FROM [dbo].[Order] AS [t0]
ORDER BY [t0].[CustomerID], [t0].[Date]

请注意,不会忽略orderby a.Date.这两个术语都包含在ORDER BY子句中,但顺序与您预期的相反.

Note that the orderby a.Date is not ignored. Both terms are included in the ORDER BY clause, but in the opposite order than you might have intended.

这篇关于Linq-to-SQL Orderby thenby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆