排序依据的正确行为依据、正确、行为

2023-09-05 23:28:41 作者:时光如酒你如狗

我也遇到了一些困惑我,我希望看到你对此事的看法。事实证明,LINQ to SQL和实体框架威胁,连续的顺序由是不同的。

I have encountered something that puzzles me and I would like to see your opinion on the matter. It turns out that linq to sql and entity framework threats consecutive order by's differently.

下面code是只用于例子,我不是说有任何意义的:

The following code is used just for example and I am not claiming it has any sense at all:

的LINQ to SQL:

Linq to sql:

DataClasses1DataContext db = new DataClasses1DataContext();
        var result = (from c in db.Products
                      orderby c.ProductName
                      orderby c.UnitPrice
                      orderby c.UnitsOnOrder
                      select c).ToList();

什么是generats在服务器端:

What it generats on the server side:

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
ORDER BY [t0].[UnitsOnOrder], [t0].[UnitPrice], [t0].[ProductName]

在相同的测试与实体框架生成的:

The same test with Entity Framework generates this:

    SELECT 
[Extent1].[ProductID] AS [ProductID], 
[Extent1].[ProductName] AS [ProductName], 
[Extent1].[SupplierID] AS [SupplierID], 
[Extent1].[CategoryID] AS [CategoryID], 
[Extent1].[QuantityPerUnit] AS [QuantityPerUnit], 
[Extent1].[UnitPrice] AS [UnitPrice], 
[Extent1].[UnitsInStock] AS [UnitsInStock], 
[Extent1].[UnitsOnOrder] AS [UnitsOnOrder], 
[Extent1].[ReorderLevel] AS [ReorderLevel], 
[Extent1].[Discontinued] AS [Discontinued]
FROM [dbo].[Products] AS [Extent1]
ORDER BY [Extent1].[UnitsOnOrder] ASC

正如你所看到的LINQ to SQL增加了通过的所有请求的订单,其中最后一个具有最高优先级(这在我看来是正确的)。 在另一方面实体框架尊重只有最后一个ORDER BY和无视所有其他人。

As you can see Linq To Sql adds all the requested order by's where the last one has the highest priority (which in my opinion is correct). On the other hand entity framework respects only the last order by and disregards all the others.

现在我知道有一个ORDER BY然后BY子句可以使用,但我只是想知道哪些行为是比较正确的。还据我记得在ASP使用的查询扩展器正在处理由如果施加在从不同的数据源生成的查询将不能正常工作的独立顺序(按照上述实施例的顺序通过的一个将被省略)

Now I know there is an order by then by clause that can be used but I am just wondering which behavior is more correct. Also as far as I remember the query extenders used in asp are working with a separate order by which if applied on a query generated from a different data source will not work correctly (according to the above example one of the order by's will be omitted)

推荐答案

我的看法是,EF是正确的。我不知道为什么L2S会做你所描述的东西 - 在我看来,如果添加使用 ThenBy ,它应该覆盖所有现有排序依据秒。

My opinion is that EF is correct. I don't know why L2S would do what you're describing - in my opinion, if you add an OrderBy clause instead of using ThenBy, it should overwrite any existing OrderBys.

当你使用LINQ到对象,你应该看到排序依据更换任何previous的,所以它更有意义,我有数据驱动LINQ作用相同。

When you're working with Linq-To-Objects, you should see OrderBy replace any previous ones, so it makes more sense to me to have the data-driven LINQ act the same.

如果行为改变你所描述的方式,那么看起来,微软同意,因为EF的设计,以取代L2S。

If the behavior changed the way you're describing, then it seems that Microsoft agrees, since EF was designed to replace L2S.