PetaPoco Orderby动态创建列

时间:2014-04-07 14:17:01

标签: c# asp.net sql petapoco

是否可以通过动态创建的列来排序(我希望按 AreaPriority 排序,但是使用此查询我得到错误无效的列名:AreaPriority,当我在SQL Server管理中运行此查询时工作室一切正常,但在petapoco运行错误)。以下是此选择查询:

SELECT strGet.StrengthId as AreaId, strGet.[Description] as AreaName,
(SELECT SUM(ias.Score) FROM ImprovementAreaScore ias WHERE ias.StrengthId=strGet.StrengthId) as AreaPriority
FROM
{0}.Strength strGet
WHERE
strGet.[Description] like '%' + @0 and strGet.SelfAssessmentId=@1 and strGet.IsStrength=0 and strGet.ToImprovementProject=1 ORDER BY AreaPriority", ...

2 个答案:

答案 0 :(得分:0)

我很确定你不能在列列表中添加一个子选择。

你应该加入....

SELECT 
    strGet.StrengthId as AreaId, 
    strGet.[Description] as AreaName, 
    SUM(ias.Score) as AreaPriority

FROM {0}.Strength strGet

JOIN ImprovementAreaScore as ias ON ias.StrengthId=strGet.StrengthId

WHERE
    strGet.[Description] like '%' + @0 
and strGet.SelfAssessmentId=@1 
and strGet.IsStrength=0 
and strGet.ToImprovementProject=1 

ORDER BY AreaPriority", ...

答案 1 :(得分:0)

获得不同的结果是很奇怪的,因为PetaPoco它只通过ADO.net运行查询,但您可以通过表达式(而不是列名称)解决问题排序,就像在旧的SQL版本中一样:

SELECT strGet.StrengthId as AreaId, strGet.[Description] as AreaName,
(SELECT SUM(ias.Score) FROM ImprovementAreaScore ias WHERE ias.StrengthId=strGet.StrengthId) as AreaPriority
FROM
{0}.Strength strGet
WHERE
strGet.[Description] like '%' + @0 and strGet.SelfAssessmentId=@1 and strGet.IsStrength=0 and strGet.ToImprovementProject=1 
ORDER BY (SELECT SUM(ias.Score) FROM ImprovementAreaScore ias WHERE ias.StrengthId=strGet.StrengthId)",