Table of Contents

Class CosmosLinqExtensions

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

This class provides extension methods for cosmos LINQ code.

public static class CosmosLinqExtensions
Inheritance
CosmosLinqExtensions
Inherited Members

Methods

AverageAsync(IQueryable<decimal>, CancellationToken)

Computes the average of a sequence of decimal values.

public static Task<Response<decimal>> AverageAsync(this IQueryable<decimal> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<decimal>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<decimal>>

The average value in the sequence.

AverageAsync(IQueryable<double>, CancellationToken)

Computes the average of a sequence of double values.

public static Task<Response<double>> AverageAsync(this IQueryable<double> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<double>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<double>>

The average value in the sequence.

AverageAsync(IQueryable<int>, CancellationToken)

Computes the average of a sequence of int values.

public static Task<Response<double>> AverageAsync(this IQueryable<int> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<int>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<double>>

The average value in the sequence.

AverageAsync(IQueryable<long>, CancellationToken)

Computes the average of a sequence of long values.

public static Task<Response<double>> AverageAsync(this IQueryable<long> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<long>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<double>>

The average value in the sequence.

AverageAsync(IQueryable<decimal?>, CancellationToken)

Computes the average of a sequence of Nullable<T> values.

public static Task<Response<decimal?>> AverageAsync(this IQueryable<decimal?> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<decimal?>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<decimal?>>

The average value in the sequence.

AverageAsync(IQueryable<double?>, CancellationToken)

Computes the average of a sequence of Nullable<T> values.

public static Task<Response<double?>> AverageAsync(this IQueryable<double?> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<double?>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<double?>>

The average value in the sequence.

AverageAsync(IQueryable<int?>, CancellationToken)

Computes the average of a sequence of Nullable<T> values.

public static Task<Response<double?>> AverageAsync(this IQueryable<int?> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<int?>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<double?>>

The average value in the sequence.

AverageAsync(IQueryable<long?>, CancellationToken)

Computes the average of a sequence of Nullable<T> values.

public static Task<Response<double?>> AverageAsync(this IQueryable<long?> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<long?>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<double?>>

The average value in the sequence.

AverageAsync(IQueryable<float?>, CancellationToken)

Computes the average of a sequence of Nullable<T> values.

public static Task<Response<float?>> AverageAsync(this IQueryable<float?> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<float?>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<float?>>

The average value in the sequence.

AverageAsync(IQueryable<float>, CancellationToken)

Computes the average of a sequence of float values.

public static Task<Response<float>> AverageAsync(this IQueryable<float> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<float>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<float>>

The average value in the sequence.

CountAsync<TSource>(IQueryable<TSource>, CancellationToken)

Returns the number of elements in a sequence.

public static Task<Response<int>> CountAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<TSource>

The sequence that contains the elements to be counted.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<int>>

The number of elements in the input sequence.

Type Parameters

TSource

The type of the elements of source.

DocumentId(object)

Returns the integer identifier corresponding to a specific item within a physical partition. This method is to be used in LINQ expressions only and will be evaluated on server. There's no implementation provided in the client library.

public static int DocumentId(this object obj)

Parameters

obj object

The root object

Returns

int

Returns the integer identifier corresponding to a specific item within a physical partition.

Examples

var documentIdQuery = documents.Where(root => root.DocumentId());

IsArray(object)

Returns a Boolean value indicating if the type of the specified expression is an array. This method is to be used in LINQ expressions only and will be evaluated on server. There's no implementation provided in the client library.

public static bool IsArray(this object obj)

Parameters

obj object

Returns

bool

Returns true if the type of the specified expression is an array; otherwise, false.

Examples

var isArrayQuery = documents.Where(document => document.Names.IsArray());

IsBool(object)

Returns a Boolean value indicating if the type of the specified expression is a boolean. This method is to be used in LINQ expressions only and will be evaluated on server. There's no implementation provided in the client library.

public static bool IsBool(this object obj)

Parameters

obj object

Returns

bool

Returns true if the type of the specified expression is a boolean; otherwise, false.

Examples

var isBoolQuery = documents.Where(document => document.IsRegistered.IsBool());

IsDefined(object)

Determines if a certain property is defined or not. This method is to be used in LINQ expressions only and will be evaluated on server. There's no implementation provided in the client library.

public static bool IsDefined(this object obj)

Parameters

obj object

Returns

bool

Returns true if this property is defined otherwise returns false.

Examples

var isDefinedQuery = documents.Where(document => document.Name.IsDefined());

IsNull(object)

Determines if a certain property is null or not. This method is to be used in LINQ expressions only and will be evaluated on server. There's no implementation provided in the client library.

public static bool IsNull(this object obj)

Parameters

obj object

Returns

bool

Returns true if this property is null otherwise returns false.

Examples

var isNullQuery = documents.Where(document => document.Name.IsNull());

IsNumber(object)

Returns a Boolean value indicating if the type of the specified expression is a number. This method is to be used in LINQ expressions only and will be evaluated on server. There's no implementation provided in the client library.

public static bool IsNumber(this object obj)

Parameters

obj object

Returns

bool

Returns true if the type of the specified expression is a number; otherwise, false.

Examples

var isNumberQuery = documents.Where(document => document.Age.IsNumber());

IsObject(object)

Returns a Boolean value indicating if the type of the specified expression is an object. This method is to be used in LINQ expressions only and will be evaluated on server. There's no implementation provided in the client library.

public static bool IsObject(this object obj)

Parameters

obj object

Returns

bool

Returns true if the type of the specified expression is an object; otherwise, false.

Examples

var isObjectQuery = documents.Where(document => document.Address.IsObject());

IsPrimitive(object)

Determines if a certain property is of primitive JSON type. This method is to be used in LINQ expressions only and will be evaluated on server. There's no implementation provided in the client library.

public static bool IsPrimitive(this object obj)

Parameters

obj object

Returns

bool

Returns true if this property is null otherwise returns false.

Examples

var isPrimitiveQuery = documents.Where(document => document.Name.IsPrimitive());

Remarks

Primitive JSON types (Double, String, Boolean and Null)

IsString(object)

Returns a Boolean value indicating if the type of the specified expression is a string. This method is to be used in LINQ expressions only and will be evaluated on server. There's no implementation provided in the client library.

public static bool IsString(this object obj)

Parameters

obj object

Returns

bool

Returns true if the type of the specified expression is a string; otherwise, false.

Examples

var isStringQuery = documents.Where(document => document.Name.IsString());

MaxAsync<TSource>(IQueryable<TSource>, CancellationToken)

Returns the maximum value in a generic IQueryable<T>.

public static Task<Response<TSource>> MaxAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<TSource>

A sequence of values to determine the maximum of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<TSource>>

The maximum value in the sequence.

Type Parameters

TSource

The type of the elements of source.

MinAsync<TSource>(IQueryable<TSource>, CancellationToken)

Returns the minimum value in a generic IQueryable<T>.

public static Task<Response<TSource>> MinAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<TSource>

A sequence of values to determine the minimum of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<TSource>>

The minimum value in the sequence.

Type Parameters

TSource

The type of the elements of source.

RegexMatch(object, string)

Returns a Boolean value indicating if the specified expression matches the supplied regex pattern. For more information, see https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/regexmatch. This method is to be used in LINQ expressions only and will be evaluated on server. There's no implementation provided in the client library.

public static bool RegexMatch(this object obj, string regularExpression)

Parameters

obj object
regularExpression string

A string expression with a regular expression defined to use when searching.

Returns

bool

Returns true if the string matches the regex expressions; otherwise, false.

Examples

var matched = documents.Where(document => document.Name.RegexMatch(<regex>));

RegexMatch(object, string, string)

Returns a Boolean value indicating if the specified expression matches the supplied regex pattern. For more information, see https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/regexmatch. This method is to be used in LINQ expressions only and will be evaluated on server. There's no implementation provided in the client library.

public static bool RegexMatch(this object obj, string regularExpression, string searchModifier)

Parameters

obj object
regularExpression string

A string expression with a regular expression defined to use when searching.

searchModifier string

An optional string expression with the selected modifiers to use with the regular expression.

Returns

bool

Returns true if the string matches the regex expressions; otherwise, false.

Examples

var matched = documents.Where(document => document.Name.RegexMatch(<regex>, <search_modifier>));

SumAsync(IQueryable<decimal>, CancellationToken)

Computes the sum of a sequence of decimal values.

public static Task<Response<decimal>> SumAsync(this IQueryable<decimal> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<decimal>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<decimal>>

The average value in the sequence.

SumAsync(IQueryable<double>, CancellationToken)

Computes the sum of a sequence of double values.

public static Task<Response<double>> SumAsync(this IQueryable<double> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<double>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<double>>

The average value in the sequence.

SumAsync(IQueryable<int>, CancellationToken)

Computes the sum of a sequence of int values.

public static Task<Response<int>> SumAsync(this IQueryable<int> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<int>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<int>>

The average value in the sequence.

SumAsync(IQueryable<long>, CancellationToken)

Computes the sum of a sequence of long values.

public static Task<Response<long>> SumAsync(this IQueryable<long> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<long>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<long>>

The average value in the sequence.

SumAsync(IQueryable<decimal?>, CancellationToken)

Computes the sum of a sequence of Nullable<T> values.

public static Task<Response<decimal?>> SumAsync(this IQueryable<decimal?> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<decimal?>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<decimal?>>

The average value in the sequence.

SumAsync(IQueryable<double?>, CancellationToken)

Computes the sum of a sequence of Nullable<T> values.

public static Task<Response<double?>> SumAsync(this IQueryable<double?> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<double?>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<double?>>

The average value in the sequence.

SumAsync(IQueryable<int?>, CancellationToken)

Computes the sum of a sequence of Nullable<T> values.

public static Task<Response<int?>> SumAsync(this IQueryable<int?> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<int?>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<int?>>

The average value in the sequence.

SumAsync(IQueryable<long?>, CancellationToken)

Computes the sum of a sequence of Nullable<T> values.

public static Task<Response<long?>> SumAsync(this IQueryable<long?> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<long?>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<long?>>

The average value in the sequence.

SumAsync(IQueryable<float?>, CancellationToken)

Computes the sum of a sequence of Nullable<T> values.

public static Task<Response<float?>> SumAsync(this IQueryable<float?> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<float?>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<float?>>

The average value in the sequence.

SumAsync(IQueryable<float>, CancellationToken)

Computes the sum of a sequence of float values.

public static Task<Response<float>> SumAsync(this IQueryable<float> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<float>

A sequence of values to calculate the average of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Response<float>>

The average value in the sequence.

ToFeedIterator<T>(IQueryable<T>)

This extension method gets the FeedIterator from LINQ IQueryable to execute query asynchronously. This will create the fresh new FeedIterator when called.

public static FeedIterator<T> ToFeedIterator<T>(this IQueryable<T> query)

Parameters

query IQueryable<T>

the IQueryable{T} to be converted.

Returns

FeedIterator<T>

An iterator to go through the items.

Type Parameters

T

the type of object to query.

Examples

This example shows how to get FeedIterator from LINQ.

IOrderedQueryable<ToDoActivity> linqQueryable = this.Container.GetItemLinqQueryable<ToDoActivity>();
using (FeedIterator<ToDoActivity> setIterator = linqQueryable.Where(item => (item.taskNum < 100)).ToFeedIterator()

ToQueryDefinition<T>(IQueryable<T>)

This method generate query definition from LINQ query.

public static QueryDefinition ToQueryDefinition<T>(this IQueryable<T> query)

Parameters

query IQueryable<T>

the IQueryable{T} to be converted.

Returns

QueryDefinition

The queryDefinition which can be used in query execution.

Type Parameters

T

the type of object to query.

Examples

This example shows how to generate query definition from LINQ.

IQueryable<T> queryable = container.GetItemsQueryIterator<T>(allowSynchronousQueryExecution = true)
                     .Where(t => b.id.contains("test"));
QueryDefinition queryDefinition = queryable.ToQueryDefinition();

ToStreamIterator<T>(IQueryable<T>)

This extension method gets the FeedIterator from LINQ IQueryable to execute query asynchronously. This will create the fresh new FeedIterator when called.

public static FeedIterator ToStreamIterator<T>(this IQueryable<T> query)

Parameters

query IQueryable<T>

the IQueryable{T} to be converted.

Returns

FeedIterator

An iterator to go through the items.

Type Parameters

T

the type of object to query.

Examples

This example shows how to get FeedIterator from LINQ.

IOrderedQueryable<ToDoActivity> linqQueryable = this.Container.GetItemLinqQueryable<ToDoActivity>();
using (FeedIterator setIterator = linqQueryable.Where(item => (item.taskNum < 100)).ToStreamIterator())