ravendb / bootcamp

107 stars 34 forks source link

Unit 1, Lesson 6: foreach statement cannot operate on variables of type 'IRawDocumentQuery<Order>' #81

Closed alexeykorsakov closed 3 years ago

alexeykorsakov commented 3 years ago

foreach (var order in orders) { WriteLine($"{order.Id} - {order.OrderedAt}"); }

Error CS1579 foreach statement cannot operate on variables of type 'IRawDocumentQuery' because 'IRawDocumentQuery' does not contain a public instance or extension definition for 'GetEnumerator'

I can fix it just with orders.ToList()

What is recommended approach?

ppekrol commented 3 years ago

Hi,

the ToList or ToArray (and friends) will materialize the query. They also show greatly where the boundary between building RavenDB query ends and pure C# LINQ begins. Something that you need to be aware of is that it keeps all the results in the memory, if you are expecting to have many of them, you can always use streaming feature (https://ravendb.net/docs/article-page/5.1/csharp/client-api/session/querying/how-to-stream-query-results)

Hope that this helps.

ayende commented 3 years ago

Fixed the code