Question

Examples on LINQ gives this

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)?

Was it helpful?

Solution

The query object is most likely of type 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". 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. For more information about deferred execution have a look at MSDN: Query Execution.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top