Why the order of LINQ to objects methods counts

前端 未结 5 2102
一整个雨季
一整个雨季 2021-02-14 15:58

I read this question\'s answers that explain the order of the LINQ to objects methods makes a difference. My question is why?

If I write a LINQ to SQL query, it doesn\'t

5条回答
  •  太阳男子
    2021-02-14 16:26

    Because, with LINQ for SQL, the SQL grammar for SELECT mandates that the different clauses occur in a particular sequence. The compiler must generate grammatically correct SQL.

    Applying LINQ for objects on an IEnumerable involves iterating over the IEnumerable and applying a sequence of actions to each object in the IEnumerable. Order matters: some actions may transform the object (or the stream of objects itself), others may throw objects away (or inject new objects into the stream).

    The compiler can't divine your intent. It builds code that does what you said to do in the order in which you said to do it.

提交回复
热议问题