Table of Contents

Class QueryDefinition

Namespace
Microsoft.Azure.Cosmos
Assembly
Microsoft.Azure.Cosmos.Client.dll

Defines a Cosmos SQL query

public class QueryDefinition
Inheritance
QueryDefinition
Inherited Members
Extension Methods

Constructors

QueryDefinition(string)

Create a QueryDefinition

public QueryDefinition(string query)

Parameters

query string

A valid Cosmos SQL query "Select * from test t"

Examples

QueryDefinition query = new QueryDefinition(
    "select * from t where t.Account = @account")
    .WithParameter("@account", "12345");

Properties

QueryText

Gets the text of the Azure Cosmos DB SQL query.

public string QueryText { get; }

Property Value

string

The text of the SQL query.

Methods

GetQueryParameters()

Returns the names and values of parameters in this QueryDefinition.

public IReadOnlyList<(string Name, object Value)> GetQueryParameters()

Returns

IReadOnlyList<(string Name, object Value)>

A list of name/value tuples representing the parameters of this QueryDefinition.

WithParameter(string, object)

Add parameters to the SQL query

public QueryDefinition WithParameter(string name, object value)

Parameters

name string

The name of the parameter.

value object

The value for the parameter.

Returns

QueryDefinition

An instance of QueryDefinition.

Examples

QueryDefinition query = new QueryDefinition(
    "select * from t where t.Account = @account")
    .WithParameter("@account", "12345");

Remarks

If the same name is added again it will replace the original value

WithParameterStream(string, Stream)

Add parameters with Stream Value to the SQL query.

public QueryDefinition WithParameterStream(string name, Stream valueStream)

Parameters

name string

The name of the parameter.

valueStream Stream

The stream value for the parameter.

Returns

QueryDefinition

An instance of QueryDefinition.

Examples

QueryDefinition query = new QueryDefinition(
    "select * from t where t.Account = @account")
    .WithParameterStream("@account", streamValue);

Remarks

UseCase : This is useful in cases like running a Query on Encrypted Values, where the value is generated post serialization and then encrypted and we don't want to change the cipher value due to a call to serializer again. If the same name is added again it will replace the original value.