LINQ查询的返回类型是什么? [英] What's the return type of a LINQ query?

查看:678
本文介绍了LINQ查询的返回类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IEnumerable<T>.据我所知,引用始终指向类实例. LINQ查询真正指向哪种实例类型?

解决方案

您可以通过在IEnumerable<T>变量上调用.GetType() 并在调试器中检查类型来找到它.

对于不同的LINQ提供程序,甚至不同的LINQ方法,此类类型可能相同也可能不同.

对您的代码重要的是它们都实现了您应该使用的IEnumerable<T>,或者还接受了表达式IQueryable<T>,这意味着您的谓词和预测将成为语法树,并且可能在运行时由LINQ提供程序操纵,例如转换成SQL.

如果您要问的是

Actual 类,甚至可能是编译器生成的,例如yield return表达式被翻译成这样的类. 不管哪种方式,它们通常是内部的,并且永远不要永远依赖它们.

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

解决方案

You can find it out by calling .GetType() on your IEnumerable<T> 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<T> which you should work with, or IQueryable<T> 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.

这篇关于LINQ查询的返回类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆