我有以下代码:
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,3 4)。给定查询的想法是在2个集合中获取所有小于当前ID的所有ID,然后将它们连接起来,以降序对第二个ID排序(金字塔的顶部重复,但是我稍后会返回它)。 / p>
问题在于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
答案 0 :(得分:2)
好像您遇到了EF6查询翻译错误-.Concat
(SQL UNION ALL
)子查询的顺序将被忽略。
我发现的解决方法是将伪造的限制运算符(如Take(int.MaxValue)
附加到子查询中,这导致EF6考虑排序:
hill =
(from company in db.Companies
where x.ID_comp > company.ID_comp
orderby company.ID_comp
select new { h = company.ID_comp.ToString() }
).Take(int.MaxValue)
.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() }
).Take(int.MaxValue))
.Select(y => y.h)
生成的SQL:
SELECT
[Extent1].[ID_comp] AS [ID_comp],
[Project5].[C2] AS [C1],
[Project5].[C1] AS [C2]
FROM [dbo].[Company] AS [Extent1]
OUTER APPLY (SELECT
[UnionAll1].[C1] AS [C1],
1 AS [C2]
FROM (SELECT TOP (2147483647)
[Project1].[C1] AS [C1]
FROM ( SELECT
[Extent2].[ID_comp] AS [ID_comp],
CAST( [Extent2].[ID_comp] AS nvarchar(max)) AS [C1]
FROM [dbo].[Company] AS [Extent2]
WHERE [Extent1].[ID_comp] > [Extent2].[ID_comp]
) AS [Project1]
ORDER BY [Project1].[ID_comp] ASC
UNION ALL
SELECT TOP (2147483647)
[Project3].[C1] AS [C1]
FROM ( SELECT
[Extent3].[ID_comp] AS [ID_comp],
CAST( [Extent3].[ID_comp] AS nvarchar(max)) AS [C1]
FROM [dbo].[Company] AS [Extent3]
WHERE [Extent1].[ID_comp] > [Extent3].[ID_comp]
) AS [Project3]
ORDER BY [Project3].[ID_comp] DESC) AS [UnionAll1] ) AS [Project5]
ORDER BY [Extent1].[ID_comp] ASC, [Project5].[C2] ASC
答案 1 :(得分:0)
var comp = db.Companies.Select(x => new
{
topid = x.ID_comp,
hill = ((from company in db.Companies.OrderBy(y => y.ID_comp)
where x.ID_comp > company.ID_comp
select new { h = company.ID_comp.ToString() })
.Concat((
from company in db.Companies.OrderByDescending(y => y.ID_comp)
where x.ID_comp > company.ID_comp
select new { h = company.ID_comp.ToString() })))
.Select(y => y.h)
});