DocumentDB SQL Linq to SQL Translation - DocumentDB SQL

What is the Link to SQL Translation in DocumentDB SQL?

In DocumentDB , SQL is used to query the documents. In .NET development ,there is LINQ provider which can generate appropriate SQL from LINQ query.

Supported Data Types

In DocumentDB, the LINQ provider included with the DocumentDB .NET SDK supports all the JSON primitive types which are as follows :

  • Numeric
  • Boolean
  • String
  • Null

Supported Expression

The scalar expressions supported in the LINQ provider included with the DocumentDB .NET SDK are as follows:

  • Constant Values : Includes constant values of the primitive data types.
  • Property/Array Index Expressions : Expressions refer to the property of an object or an array element.
  • Arithmetic Expressions : Includes common arithmetic expressions on numerical and Boolean values.
  • String Comparison Expression : Includes comparing a string value to some constant string value.
  • Object/Array Creation Expression : Returns an object of compound value type or anonymous type or an array of such objects. These values can be nested.

Supported LINQ Operators

The list of LINQ operators supported in the LINQ provider included with the DocumentDB .NET SDK are:

  • Select : Projections translate to the SQL SELECT including object construction.
  • Where : Filters translate to the SQL WHERE, and support translation between && , || and ! to the SQL operators.
  • SelectMany : Allows unwinding of arrays to the SQL JOIN clause. Can be used to chain/nest expressions to filter array elements.
  • OrderBy and OrderByDescending : Translates to ORDER BY ascending/descending.
  • CompareTo : Translates to range comparisons. Commonly used for strings since they’re not comparable in .NET.
  • Take : Translates to the SQL TOP for limiting results from a query.
  • Math Functions : Supports translation from .NET’s Abs, Acos, Asin, Atan, Ceiling, Cos, Exp, Floor, Log, Log10, Pow, Round, Sign, Sin, Sqrt, Tan, Truncate to the equivalent SQL built-in functions.
  • String Functions : Supports translation from .NET’s Concat, Contains, EndsWith, IndexOf, Count, ToLower, TrimStart, Replace, Reverse, TrimEnd, StartsWith, SubString, ToUpper to the equivalent SQL built-in functions.
  • Array Functions : Supports translation from .NET’s Concat, Contains, and Count to the equivalent SQL built-in functions.
  • Geospatial Extension Functions : Supports translation from stub methods Distance, Within, IsValid, and IsValidDetailed to the equivalent SQL built-in functions.
  • User-Defined Extension Function : Supports translation from the stub method UserDefinedFunctionProvider.Invoke to the corresponding user-defined function.
  • Miscellaneous : Supports translation of coalesce and conditional operators. Can translate Contains to String CONTAINS, ARRAY_CONTAINS or the SQL IN depending on context.

The usage the .Net SDK is illustrated in the following example. Following are the three documents which we will be consider for this example.

New Customer 1

New Customer 2

New Customer 3

The query using LINQ is explained in the following code. We've defined a LINQ query in q, but it won't execute until we run to list.

For DocumentDB ,the SDK will convert our LINQ query into SQL syntax, generating a SELECT and WHERE clause based on our LINQ syntax.
The above queries are called from the CreateDocumentClient task.

the above code is executed and the following output is produced.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

DocumentDB SQL Topics