我有以下代码:
var comp = db.Companies.Select(x => new
{
topid = x.ID_comp,
hill = ((from company in db.Companies
where x.ID_comp > company.ID_comp
orderby company.ID_comp
select new { h = company.ID_comp.ToString() })
.Concat((
from company in db.Companies
where x.ID_comp > company.ID_comp
orderby company.ID_comp descending
select new { h = company.ID_comp.ToString() })))
.Select(y => y.h)
});
我们的任务是为每个公司的id构建id的“金字塔”:121
代表3
,12321
代表4
等等(如果有公司的id是1,2,34)。给定查询的思想是获取两个集合中小于当前的所有it,然后将它们连接在一起,按降序排列第二个集合(金字塔的顶部重复,但稍后我将返回到它)。
问题是没有为每个集合单独执行order by
子句,最后我得到了id为4的公司的1 2 3 1 2 3
之类的东西。如何才能以不同的顺序对嵌套查询进行排序?
UPD1:我使用的数据:
表db.Companies
ID_comp | name
--------------------
1 |Don_avia
2 |Aeroflot
3 |Dale_avia
4 |air_France
5 |British_AW
输出:
1:
2: 1 1
3: 1 2 1 2
4: 1 2 3 1 2 3
5: 1 2 3 4 1 2 3 4
UPD2:结果查询:
SELECT
[Extent1].[ID_comp] AS [ID_comp],
[Project3].[C2] AS [C1],
[Project3].[C1] AS [C2]
FROM [dbo].[Company] AS [Extent1]
OUTER APPLY (SELECT
[UnionAll1].[C1] AS [C1],
1 AS [C2]
FROM (SELECT
CAST( [Extent2].[ID_comp] AS nvarchar(max)) AS [C1]
FROM [dbo].[Company] AS [Extent2]
WHERE [Extent1].[ID_comp] > [Extent2].[ID_comp]
UNION ALL
SELECT
CAST( [Extent3].[ID_comp] AS nvarchar(max)) AS [C1]
FROM [dbo].[Company] AS [Extent3]
WHERE [Extent1].[ID_comp] > [Extent3].[ID_comp]) AS [UnionAll1] ) AS [Project3]
ORDER BY [Extent1].[ID_comp] ASC, [Project3].[C2] ASC
https://stackoverflow.com/questions/56313165
复制相似问题