concept LINQ in category ravendb

appears as: LINQ
RavenDB in Action

This is an excerpt from Manning's book RavenDB in Action.

RavenDB uses LINQ extensively; therefore this book relies on you being comfortable using LINQ as well. For your convenience, following is a quick primer to LINQ. By no means a complete or a thorough guide, just something to get you started if you are new to this wonderful query language. If you need a refresher a good resource is the book LINQ in Action by Manning or “101 LINQ Samples” at http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b. If you’ve already used LINQ a few times, you can safely skip this.

What is LINQ? LINQ is a collection of extension methods that act on lists, where a list is any .NET data structure implementing the IEnumerable interface. These methods filter lists, sort lists, and in many other ways help manipulate the individual items in a list.

1.4.6   Querying using LINQ

The most interesting feature of a database is probably its querying abilities, and RavenDB will not disappoint you here. By using LINQ as its querying language in the Client API, RavenDB gets slick and powerful querying abilities. Almost the entire LINQ grammar is available, making strongly typed queries easy to write and read, yet keeping them powerful.

using (var session = docStore.OpenSession()) { var allBooks = session.Query<Book>().ToList(); #B var jkRowlingBooks = session.Query<Book>() .Where(x => x.Author.Equals("J.K. Rowling")) .ToList(); var cheapBooks = session.Query<Book>() .Where(x => x.Price < 10) .ToList(); #A } #A Important: without starting to iterate through the results or calling ToList() or ToArray() on the query, the query won’t get executed. Sometimes we tend to forget LINQ’s deferred query execution. #B Being safe by default, RavenDB will actively limit the amount of results to 128 by default, so this will never provide more than 128 results, even if there are a dozen times that amount of results satisfying the query. If you need more than 128 results, you should use paging, which we discuss in the next chapter.