2013-05-03 61 views
-3

我不明白为什么代码A是正确的和代码B不正确:了解LINQ查询 - 排序依据上升

码A

IEnumerable<decimal> loanQuery = 
from amount in loanAmounts 
where amount % 2 == 0 
orderby amount 
select ascending amount //this line 

代码B(不正确)

IEnumerable<decimal> loanQuery = 
from amount in loanAmounts 
where amount % 2 == 0 
select amount 
orderby ascending amount 

由于很多人的回答都不正确,我现在发布了正确的代码:

IEnumerable<decimal> loanQuery = 
from amount in loanAmounts 
where amount % 2 == 0 
orderby amount ascending 
select amount 
+0

因为您需要用select完成查询。 – Maarten 2013-05-03 09:05:06

+4

在编程语言中有一种叫做语法的东西 – Cris 2013-05-03 09:05:47

+0

http://msdn.microsoft.com/en-IN/library/bb397678.aspx – Freelancer 2013-05-03 09:06:13

回答

9

一个LINQ查询不是一个SQL查询,所以它有自己的语法规则。你必须遵循的顺序:

FROM  
WHERE  
ORDER BY  
SELECT  
GROUP BY 

其同样的原因,为什么你不能写的SQL语句:

SELECT * WHERE i=2 FROM tableName 

,但必须写

SELECT * FROM tableName WHERE i=2 
+0

谢谢。这有帮助。我一定在文档中错过了。 – Neeta 2013-05-03 09:10:59

3

简而言之:它的语法是必需的。请参阅Microsoft文档here

的查询表达式必须从第一个开始,且必须与 选择或group子句结束。

1

From MSDN doc,你看到了什么正确的订单为关键字

查询表达式必须以from子句开头,并且必须以 select或group子句结尾。在第一个从句子和最后一个 select或group子句之间,它可以包含一个或多个这些可选的 子句:where,orderby,join,let,甚至还有其他子句。 也可以使用into关键字将联接的结果或 group子句用作相同查询表达式中 中其他查询子句的来源。