为什么 "linq to sql" 查询以 FROM 关键字开头,这与常规 SQL 查询不同?

Why do "linq to sql" queries starts with the FROM keyword unlike regular SQL queries?

为什么 linq to sql 查询以 FROM 关键字开头,这与常规 SQL 查询不同?

LINQ 在 SQL 中模仿 Logical Query processing 你有:

8. SELECT
9. DISTINCT
11. TOP
1. FROM
2. ON
3. JOIN
4. WHERE
5. GROUP BY
6. WITH CUBE/ROLLUP
7. HAVING
10. ORDER BY
12. OFFSET/FETCH

但实际执行起来是这样的:

1. FROM
2. ON
3. JOIN
4. WHERE
5. GROUP BY
6. WITH CUBE/ROLLUP
7. HAVING
8. SELECT
9. DISTINCT
10. ORDER BY
11. TOP
12. OFFSET/FETCH

很多人没有意识到这一点,并犯了一些简单的错误,例如:

SELECT col AS alias_name
FROM tab
WHERE aliass_name > 10;

并询问为什么它不起作用。因为他们假设订单就像他们写的一样。 LINQ 更擅长这件事。

另见 Logical Query Processing and BOL:

Logical Processing Order of the SELECT statement

The following steps show the logical processing order, or binding order, for a SELECT statement. This order determines when the objects defined in one step are made available to the clauses in subsequent steps. For example, if the query processor can bind to (access) the tables or views defined in the FROM clause, these objects and their columns are made available to all subsequent steps. Conversely, because the SELECT clause is step 8, any column aliases or derived columns defined in that clause cannot be referenced by preceding clauses. However, they can be referenced by subsequent clauses such as the ORDER BY clause. Note that the actual physical execution of the statement is determined by the query processor and the order may vary from this list.

FROM

ON

JOIN

WHERE

GROUP BY

WITH CUBE or WITH ROLLUP

HAVING

SELECT

DISTINCT

ORDER BY

TOP

关于您的问题的一些文档可用here and they says that It is more similar to "foreach". However this 是开始和了解什么是 LINQ 和不同类型的 LINQ 的好地方。

您通常会发现以下类型的 LINQ 无处不在。 Read here.

  1. LINQ (Linq to Objects)
  2. DLINQ (Linq to SQL)
  3. XLINQ (Linq to XML)