C# LINQ lambda-OrderByDescending正在添加不需要的选择

C# LINQ lambda-OrderByDescending正在添加不需要的选择,c#,sql-server,linq,lambda,sql-order-by,C#,Sql Server,Linq,Lambda,Sql Order By,我正在处理一个非常疯狂的lambda查询。以下是我的初始LINQ lambda语句,未按排序/排序: var query = orders.Join(customers, o => o.CustomerID, c => c.ID, (o, c) => new { o, c }) .Join(ordersections, o => o.o.ID, os => os.OrderID, (o, os) => new { o.o, o.

我正在处理一个非常疯狂的lambda查询。以下是我的初始LINQ lambda语句,未按排序/排序:

var query = orders.Join(customers, o => o.CustomerID, c => c.ID, (o, c) => new { o, c })
                .Join(ordersections, o => o.o.ID, os => os.OrderID, (o, os) => new { o.o, o.c, os })
                .Join(tickets, o => o.os.ID, t => t.OrderSectionID, (o, t) => new { o.o, o.c, o.os, t })
                .Join(events, o => o.t.EventID, e => e.id, (o, e) => new { o.o, o.c, o.os, o.t, e })
                .Join(clients, o => o.e.ClientID, cl => cl.id, (o, cl) => new { o.o, o.c, o.os, o.t, o.e, cl })
                .Join(venues, o => o.e.VenueID, v => v.VenueID, (o, v) => new ModelsCs.LINQ.CustomerSearchResult { order = o.o, customer = o.c, orderSection = o.os, ticket = o.t, evt = o.e, client = o.cl, venue = v })
                         .AsExpandable()
                         .Where(predicate) // from PredicateBuilder
                         .GroupBy(x => new
                         {
                             // variables to group by

                         })                         
                         .Select(s => new CustomerSearchResult
                         {
                             // Selecting the variables, all good and fun!
                         });
生成的SQL如下所示:

SELECT <correct variables to select> 
FROM [dbo].[Order] AS [t0]
INNER JOIN [dbo].[Customer] AS [t1] ON [t0].[Customer] = ([t1].[Customer])
INNER JOIN [dbo].[OrderSection] AS [t2] ON [t0].[Order] = [t2].[Order]
INNER JOIN [dbo].[Ticket] AS [t3] ON [t2].[OrderSection] = [t3].[OrderSection]
INNER JOIN [dbo].[Event] AS [t4] ON [t3].[Event] = [t4].[Event]
INNER JOIN [dbo].[Client] AS [t5] ON [t4].[Client] = ([t5].[Client])
INNER JOIN [dbo].[Venue] AS [t6] ON [t4].[Venue] = ([t6].[Venue])
WHERE ([t5].[Brand] = @p0)
    AND ([t0].[Brand] = @p1)
    AND ([t4].[EventStart] >= @p2)
    AND ([t0].[OrderDateTime] >= @p3)
    AND ([t1].[email] LIKE @p4)
GROUP BY <correct group by variables>
^^^^这就是我想做的

以下是我已经尝试过的:

所以我试着在我的LINQ lambda声明中添加以下内容:

.OrderByDescending(x => x.SortingVariable1)
但这是现在生成的SQL代码:

SELECT <correct variables to select>
FROM (
    SELECT <correct GROUP BY variables>
    FROM [dbo].[Order] AS [t0]
    INNER JOIN [dbo].[Customer] AS [t1] ON [t0].[Customer] = ([t1].[Customer])
    INNER JOIN [dbo].[OrderSection] AS [t2] ON [t0].[Order] = [t2].[Order]
    INNER JOIN [dbo].[Ticket] AS [t3] ON [t2].[OrderSection] = [t3].[OrderSection]
    INNER JOIN [dbo].[Event] AS [t4] ON [t3].[Event] = [t4].[Event]
    INNER JOIN [dbo].[Client] AS [t5] ON [t4].[Client] = ([t5].[Client])
    INNER JOIN [dbo].[Venue] AS [t6] ON [t4].[Venue] = ([t6].[Venue])
    WHERE ([t5].[Brand] = @p0)
        AND ([t0].[Brand] = @p1)
        AND ([t4].[EventStart] >= @p2)
        AND ([t0].[OrderDateTime] >= @p3)
        AND ([t1].[email] LIKE @p4)
    GROUP BY <correct group by variables>
    ) AS [t7]
ORDER BY [t7].[SortingVariable1] DESC
无论我在lambda语句中放在哪里,OrderByDescending都不能正常工作

我的问题:有人知道我如何修改我的LINQ Lambda语句,通过将variable1 DESC排序到生成的SQL语句的末尾来正确地添加订单吗?

它是通过正确地添加订单。自动生成的代码的本质是,它通常不会像人工生成的代码那样漂亮。它编写的内容通常会更加冗长,因为生成这样的代码通常更容易

如果您想要确切地拥有一组特定的SQL代码,则需要手工编写。如果您想让它自动为您生成,那么您必须满足于不太漂亮但完全正确且功能相同的代码。

外部选择本身不是问题,因为它不会带来额外的开销。通过添加嵌套,SQL generator可以对任何返回的字段(甚至是计算字段)进行排序,而无需包含两次计算

此行为是由于以下示例所示的SQL限制造成的:

SELECT A+B as A_plus_B
FROM MyTable
ORDER BY A_plus_B -- <=== This does not work
必须重新编写上面的查询,或者重复计算两次,即

SELECT A+B as A_plus_B
FROM MyTable
ORDER BY A+B -- <=== Computation is repeated
或使用嵌套查询或CTE:

SELECT A_plusB FROM (
    SELECT A+B as A_plus_B
    FROM MyTable
)
ORDER BY A_plus_B -- <=== This works

LINQ的SQL生成器采用第二种方法,生成您看到的语句。

我看不出问题所在?我不会担心这个额外的选择。我更关心的是你如何获得数据,如果你使用EF,那么使用导航属性,你说它不能正常工作是什么意思?查询是否没有返回所需的内容,或者顺序与指定的顺序不匹配,或者只是得到了更复杂的SQL?
SELECT A_plusB FROM (
    SELECT A+B as A_plus_B
    FROM MyTable
)
ORDER BY A_plus_B -- <=== This works