What's the return type of a LINQ query?

后端 未结 5 611
轻奢々
轻奢々 2021-02-11 22:20

Is it IEnumerable. As far as I know, the reference always points to a class instance. What instance type does the LINQ query really point to?

5条回答
  •  Happy的楠姐
    2021-02-11 22:50

    You can find it out by calling .GetType() on your IEnumerable variable and inspecting the type in the debugger.

    For different LINQ providers and even different LINQ methods, such types may or may not be different.

    What matters to your code is that they all implement IEnumerable which you should work with, or IQueryable which also accepts expressions, meaning your predicates and projections will become syntax trees and may be manipulated by a LINQ provider at runtime, e.g. to be translated into SQL.

    Actual classes, if this is what you're asking about, may even be compiler-generated, e.g. yield return expression is translated to such a class.
    Either way, they are usually internal and you should never, ever depend on them.

提交回复
热议问题