简体   繁体   English

LINQ查询的结果是什么类型的?

[英]Of what type is the result of a LINQ query?

Examples on LINQ gives this LINQ上的示例给出了这一点

var query = context.Contacts
    .Where(q => q.FirstName == "Tom");

I'm wondering what object is "query"? 我想知道“查询”是什么对象? And also is it possible (advisable) to pass it to a method (within the same class)? 还有可能(建议)将其传递给方法(在同一类中)吗?

The query object is most likely of type IQueryable<Contact> . query对象很可能是IQueryable<Contact>类型的。 You can of course pass it to a method, whether that is in the same class or in another class does not matter. 您当然可以将其传递给方法,无论该方法在同一类中还是在另一类中都没有关系。

But keep in mind that LINQ does use a mechanism named "deferred execution". 但是请记住,LINQ确实使用了一种名为“延迟执行”的机制。 That means that query does not get enumerated immediately, but rather when it is needed. 这意味着查询不会立即枚举,而是在需要时枚举。 All the stuff you put in your query (the Where -clause for example) gets executed then. 然后,您在查询中放入的所有内容(例如Where -clause)都将执行。 For more information about deferred execution have a look at MSDN: Query Execution . 有关延迟执行的更多信息,请参见MSDN:查询执行

NB: You can find out the exact type of the query variable if you hover you mouse over it or the var keyword in Visual Studio. 注意:如果将鼠标悬停在query变量或Visual Studio中的var关键字上,则可以找到query变量的确切类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM