The DocumentDB .NET SDK also includes a LINQ (Language Integrated Query) provider that implements query translation of projection, filtering, traversal and sorting operators to SQL. Many .NET developers prefer LINQ based querying because it offers a single consistent programming model across how they work with objects in application code and how they express query logic running in the database. We're happy to announce the .NET SDK 1.4.0 (and later versions) include expanded support for LINQ operators comprised of Contains, String, Mathematical and Array functions.
DocumentDB LINQ to SQL:
The DocumentDB SDK supports translation of the Where() operator for filtering, Select() for projection, OrderBy() and OrderByDescending() for sorting, SelectMany() for hierarchical navigation, as well as composition and chaining of these statements. Learn more about how the mapping between LINQ and SQL works here. The following operators can now also be used within these LINQ expressions:
- Math: Abs, Acos, Asin, Atan, Ceiling, Cos, Exp, Floor, Log, Log10, Pow, Round, Sign, Sin, Sqrt, Tan, Truncate
- String: Concat, Contains, EndsWith, IndexOf, Count, ToLower, TrimStart, Replace, Reverse, TrimEnd, StartsWith, SubString, ToUpper
- Array: Concat, Contains, and Count
- Spatial: Distance, Within, IsValid, and IsValidDetailed
Get started with querying DocumentDB in LINQ by downloading the latest version of the DocumentDB .NET SDK from Nuget here. We also created a GitHub project containing code samples for querying DocumentDB using LINQ including:
- How to use these new string, mathematical and array operators with LINQ
- How to perform queries using hierarchical navigation using JOINs with LINQ's SelectMany
- How to perform asynchronous and paginated LINQ queries using the IDocumentQuery<T> interface
- How to perform sorting using OrderBy and OrderByDescending
- How to perform spatial queries in LINQ using the Within, Distance methods
If you need any help or have questions or feedback, please reach out to us on the developer forums on stack overflow or schedule a 1:1 chat with the DocumentDB engineering team. Stay up-to-date on the latest DocumentDB news and features by following us on Twitter @DocumentDB.