-3

我不明白为什么代码 A 正确而代码 B 不正确:

代码 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
4

3 回答 3

9

LINQ 查询不是 SQL 查询,因此有自己的语法规则。您必须遵循以下顺序:

FROM     
WHERE      
ORDER BY     
SELECT      
GROUP BY

不能编写 SQL 语句的原因相同:

SELECT * WHERE i=2 FROM tableName

但必须写

SELECT * FROM tableName WHERE i=2
于 2013-05-03T09:05:17.253 回答
3

简而言之:它是语法所要求的。在此处查看 Microsoft 文档

查询表达式必须以 from 子句开头,并且必须以 select 或 group 子句结尾。

于 2013-05-03T09:05:09.043 回答
2

从 MSDN doc中,您可以看到关键字的正确顺序

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

于 2013-05-03T09:07:08.983 回答