IDocumentClient.CreateDocumentQuery IDocumentClient.CreateDocumentQuery Method

Definition

Overloads

CreateDocumentQuery(String, FeedOptions) CreateDocumentQuery(String, FeedOptions)

Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB service. It returns IOrderedQueryable{Document}.

CreateDocumentQuery(Uri, FeedOptions) CreateDocumentQuery(Uri, FeedOptions)

Method to create a query for documents in the Azure Cosmos DB service.

CreateDocumentQuery(String, SqlQuerySpec, FeedOptions) CreateDocumentQuery(String, SqlQuerySpec, FeedOptions)

Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement with parameterized values. It returns an IQueryable{dynamic}. For more information on preparing SQL statements with parameterized values, please see SqlQuerySpec.

CreateDocumentQuery(String, String, FeedOptions) CreateDocumentQuery(String, String, FeedOptions)

Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement. It returns an IQueryable{dynamic}.

CreateDocumentQuery(Uri, SqlQuerySpec, FeedOptions) CreateDocumentQuery(Uri, SqlQuerySpec, FeedOptions)

Method to create a query for documents in the Azure Cosmos DB service.

CreateDocumentQuery(Uri, String, FeedOptions) CreateDocumentQuery(Uri, String, FeedOptions)

Method to create a query for documents in the Azure Cosmos DB service.

CreateDocumentQuery<T>(Uri, SqlQuerySpec, FeedOptions) CreateDocumentQuery<T>(Uri, SqlQuerySpec, FeedOptions)

Method to create a query for documents in the Azure Cosmos DB service.

CreateDocumentQuery<T>(Uri, String, FeedOptions) CreateDocumentQuery<T>(Uri, String, FeedOptions)

Method to create a query for documents in the Azure Cosmos DB service.

CreateDocumentQuery<T>(String, String, FeedOptions) CreateDocumentQuery<T>(String, String, FeedOptions)

Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement. It returns an IQueryable{T}.

CreateDocumentQuery<T>(String, SqlQuerySpec, FeedOptions) CreateDocumentQuery<T>(String, SqlQuerySpec, FeedOptions)

Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement with parameterized values. It returns an IQueryable{T}. For more information on preparing SQL statements with parameterized values, please see SqlQuerySpec.

CreateDocumentQuery<T>(Uri, FeedOptions) CreateDocumentQuery<T>(Uri, FeedOptions)

Method to create a query for documents in the Azure Cosmos DB service.

CreateDocumentQuery<T>(String, FeedOptions) CreateDocumentQuery<T>(String, FeedOptions)

Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB service.

CreateDocumentQuery(String, FeedOptions) CreateDocumentQuery(String, FeedOptions)

Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB service. It returns IOrderedQueryable{Document}.

public System.Linq.IOrderedQueryable<Microsoft.Azure.Documents.Document> CreateDocumentQuery (string collectionLink, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = null);
abstract member CreateDocumentQuery : string * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IOrderedQueryable<Microsoft.Azure.Documents.Document>

Parameters

collectionLink
String String

The link to the parent document collection.

feedOptions
FeedOptions FeedOptions

The options for processing the query result feed. For details, see FeedOptions

Returns

An IOrderedQueryable{Document} that can evaluate the query.

Examples

This example below queries for documents by id.


 Document document = client.CreateDocumentQuery<Document>(collectionLink)
     .Where(d => d.Id == "War and Peace").AsEnumerable().FirstOrDefault();

 

Remarks

This overload should be used when the schema of the queried documents is unknown or when querying by ID and replacing/deleting documents.

Since Document is a DynamicObject, it can be dynamically cast back to the original C# object.

See also

CreateDocumentQuery(Uri, FeedOptions) CreateDocumentQuery(Uri, FeedOptions)

Method to create a query for documents in the Azure Cosmos DB service.

public System.Linq.IOrderedQueryable<Microsoft.Azure.Documents.Document> CreateDocumentQuery (Uri documentCollectionUri, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = null);
abstract member CreateDocumentQuery : Uri * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IOrderedQueryable<Microsoft.Azure.Documents.Document>

Parameters

documentCollectionUri
Uri Uri

The URI of the document collection.

feedOptions
FeedOptions FeedOptions

(Optional) The FeedOptions for processing the query results feed.

Returns

The query result set.

CreateDocumentQuery(String, SqlQuerySpec, FeedOptions) CreateDocumentQuery(String, SqlQuerySpec, FeedOptions)

Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement with parameterized values. It returns an IQueryable{dynamic}. For more information on preparing SQL statements with parameterized values, please see SqlQuerySpec.

public System.Linq.IQueryable<dynamic> CreateDocumentQuery (string collectionLink, Microsoft.Azure.Documents.SqlQuerySpec querySpec, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = null);
abstract member CreateDocumentQuery : string * Microsoft.Azure.Documents.SqlQuerySpec * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<obj>

Parameters

collectionLink
String String

The link to the parent document collection.

querySpec
SqlQuerySpec SqlQuerySpec

The SqlQuerySpec instance containing the SQL expression.

feedOptions
FeedOptions FeedOptions

The options for processing the query result feed. For details, see FeedOptions

Returns

an IQueryable{dynamic> that can evaluate the query.

Examples

This example below queries for book documents.


// SQL querying allows dynamic property access
var query = new SqlQuerySpec(
    "SELECT * FROM books b WHERE b.title = @title", 
    new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "@title", Value = "War and Peace" }}));
    
dynamic document = client.CreateDocumentQuery<dynamic>(collectionLink, query).AsEnumerable().FirstOrDefault();

Remarks

Refer to https://msdn.microsoft.com/en-us/library/azure/dn782250.aspx and http://azure.microsoft.com/documentation/articles/documentdb-sql-query/ for syntax and examples.

See also

CreateDocumentQuery(String, String, FeedOptions) CreateDocumentQuery(String, String, FeedOptions)

Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement. It returns an IQueryable{dynamic}.

public System.Linq.IQueryable<dynamic> CreateDocumentQuery (string collectionLink, string sqlExpression, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = null);
abstract member CreateDocumentQuery : string * string * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<obj>

Parameters

collectionLink
String String

The link to the parent document collection.

sqlExpression
String String

The SQL statement.

feedOptions
FeedOptions FeedOptions

The options for processing the query result feed. For details, see FeedOptions

Returns

an IQueryable{dynamic> that can evaluate the query.

Examples

This example below queries for book documents.


// SQL querying allows dynamic property access
dynamic document = client.CreateDocumentQuery<dynamic>(collectionLink,
    "SELECT * FROM books b WHERE b.title == 'War and Peace'").AsEnumerable().FirstOrDefault();

Remarks

Refer to https://msdn.microsoft.com/en-us/library/azure/dn782250.aspx and http://azure.microsoft.com/documentation/articles/documentdb-sql-query/ for syntax and examples.

See also

CreateDocumentQuery(Uri, SqlQuerySpec, FeedOptions) CreateDocumentQuery(Uri, SqlQuerySpec, FeedOptions)

Method to create a query for documents in the Azure Cosmos DB service.

public System.Linq.IQueryable<dynamic> CreateDocumentQuery (Uri documentCollectionUri, Microsoft.Azure.Documents.SqlQuerySpec querySpec, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = null);
abstract member CreateDocumentQuery : Uri * Microsoft.Azure.Documents.SqlQuerySpec * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<obj>

Parameters

documentCollectionUri
Uri Uri

The URI of the document collection.

querySpec
SqlQuerySpec SqlQuerySpec

The sql query.

feedOptions
FeedOptions FeedOptions

(Optional) The FeedOptions for processing the query results feed.

Returns

The query result set.

CreateDocumentQuery(Uri, String, FeedOptions) CreateDocumentQuery(Uri, String, FeedOptions)

Method to create a query for documents in the Azure Cosmos DB service.

public System.Linq.IQueryable<dynamic> CreateDocumentQuery (Uri documentCollectionUri, string sqlExpression, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = null);
abstract member CreateDocumentQuery : Uri * string * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<obj>

Parameters

documentCollectionUri
Uri Uri

The URI of the document collection.

sqlExpression
String String

The sql query.

feedOptions
FeedOptions FeedOptions

(Optional) The FeedOptions for processing the query results feed.

Returns

The query result set.

CreateDocumentQuery<T>(Uri, SqlQuerySpec, FeedOptions) CreateDocumentQuery<T>(Uri, SqlQuerySpec, FeedOptions)

Method to create a query for documents in the Azure Cosmos DB service.

public System.Linq.IQueryable<T> CreateDocumentQuery<T> (Uri documentCollectionUri, Microsoft.Azure.Documents.SqlQuerySpec querySpec, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = null);
abstract member CreateDocumentQuery : Uri * Microsoft.Azure.Documents.SqlQuerySpec * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<'T>

Type Parameters

T

The type of object to query.

Parameters

documentCollectionUri
Uri Uri

The URI of the document collection.

querySpec
SqlQuerySpec SqlQuerySpec

The sql query.

feedOptions
FeedOptions FeedOptions

(Optional) The FeedOptions for processing the query results feed.

Returns

The query result set.

CreateDocumentQuery<T>(Uri, String, FeedOptions) CreateDocumentQuery<T>(Uri, String, FeedOptions)

Method to create a query for documents in the Azure Cosmos DB service.

public System.Linq.IQueryable<T> CreateDocumentQuery<T> (Uri documentCollectionUri, string sqlExpression, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = null);
abstract member CreateDocumentQuery : Uri * string * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<'T>

Type Parameters

T

The type of object to query.

Parameters

documentCollectionUri
Uri Uri

The URI of the document collection.

sqlExpression
String String

The sql query.

feedOptions
FeedOptions FeedOptions

(Optional) The FeedOptions for processing the query results feed.

Returns

The query result set.

CreateDocumentQuery<T>(String, String, FeedOptions) CreateDocumentQuery<T>(String, String, FeedOptions)

Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement. It returns an IQueryable{T}.

public System.Linq.IQueryable<T> CreateDocumentQuery<T> (string collectionLink, string sqlExpression, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = null);
abstract member CreateDocumentQuery : string * string * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<'T>

Type Parameters

T

The type of object to query.

Parameters

collectionLink
String String

The link to the parent collection.

sqlExpression
String String

The SQL statement.

feedOptions
FeedOptions FeedOptions

The options for processing the query result feed. For details, see FeedOptions

Returns

An IQueryable{T} that can evaluate the query.

Examples

This example below queries for some book documents.


public class Book 
{
    [JsonProperty("title")]
    public string Title {get; set;}
    
    public Author Author {get; set;}
    
    public int Price {get; set;}
}

public class Author
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
}

// Query by the Title property
Book book = client.CreateDocumentQuery<Book>(collectionLink, 
    "SELECT * FROM books b WHERE b.title  = 'War and Peace'").AsEnumerable().FirstOrDefault();

// Query a nested property
Book otherBook = client.CreateDocumentQuery<Book>(collectionLink,
    "SELECT * FROM books b WHERE b.Author.FirstName = 'Leo'").AsEnumerable().FirstOrDefault();

// Perform a range query (needs an IndexType.Range on price or FeedOptions.EnableScansInQuery)
foreach (Book matchingBook in client.CreateDocumentQuery<Book>(
    collectionLink, "SELECT * FROM books b where b.Price > 1000"))
{
    // Iterate through books
}

// Query asychronously. Optionally set FeedOptions.MaxItemCount to control page size
using (var queryable = client.CreateDocumentQuery<Book>(collectionLink, 
    "SELECT * FROM books b WHERE b.title  = 'War and Peace'", 
    new FeedOptions { MaxItemCount = 10 }).AsDocumentQuery())
{
    while (queryable.HasMoreResults) 
    {
        foreach(Book b in await queryable.ExecuteNextAsync<Book>())
        {
            // Iterate through books
        }
    }
}

Remarks

Refer to https://msdn.microsoft.com/en-us/library/azure/dn782250.aspx and http://azure.microsoft.com/documentation/articles/documentdb-sql-query/ for syntax and examples.

See also

CreateDocumentQuery<T>(String, SqlQuerySpec, FeedOptions) CreateDocumentQuery<T>(String, SqlQuerySpec, FeedOptions)

Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement with parameterized values. It returns an IQueryable{T}. For more information on preparing SQL statements with parameterized values, please see SqlQuerySpec.

public System.Linq.IQueryable<T> CreateDocumentQuery<T> (string collectionLink, Microsoft.Azure.Documents.SqlQuerySpec querySpec, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = null);
abstract member CreateDocumentQuery : string * Microsoft.Azure.Documents.SqlQuerySpec * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<'T>

Type Parameters

T

The type of object to query.

Parameters

collectionLink
String String

The link to the parent document collection.

querySpec
SqlQuerySpec SqlQuerySpec

The SqlQuerySpec instance containing the SQL expression.

feedOptions
FeedOptions FeedOptions

The options for processing the query result feed. For details, see FeedOptions

Returns

An IQueryable{T} that can evaluate the query.

Examples

This example below queries for some book documents.


public class Book 
{
    [JsonProperty("title")]
    public string Title {get; set;}
    
    public Author Author {get; set;}
    
    public int Price {get; set;}
}

public class Author
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
}

// Query using Title
Book book, otherBook;

var query = new SqlQuerySpec(
    "SELECT * FROM books b WHERE b.title = @title", 
    new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "@title", Value = "War and Peace" }}));
book = client.CreateDocumentQuery<Book>(collectionLink, query).AsEnumerable().FirstOrDefault();

// Query a nested property
query = new SqlQuerySpec(
    "SELECT * FROM books b WHERE b.Author.FirstName = @firstName", 
    new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "@firstName", Value = "Leo" }}));
otherBook = client.CreateDocumentQuery<Book>(collectionLink, query).AsEnumerable().FirstOrDefault();

// Perform a range query (needs an IndexType.Range on price or FeedOptions.EnableScansInQuery)
query = new SqlQuerySpec(
    "SELECT * FROM books b WHERE b.Price > @minPrice", 
    new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "@minPrice", Value = 1000 }}));
foreach (Book b in client.CreateDocumentQuery<Book>(
    collectionLink, query))
{
    // Iterate through books
}

// Query asychronously. Optionally set FeedOptions.MaxItemCount to control page size
query = new SqlQuerySpec(
    "SELECT * FROM books b WHERE b.title = @title", 
    new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "@title", Value = "War and Peace" }}));
    
using (var queryable = client.CreateDocumentQuery<Book>(collectionLink, query, 
    new FeedOptions { MaxItemCount = 10 }).AsDocumentQuery())
{
    while (queryable.HasMoreResults) 
    {
        foreach(Book b in await queryable.ExecuteNextAsync<Book>())
        {
            // Iterate through books
        }
    }
}

Remarks

Refer to https://msdn.microsoft.com/en-us/library/azure/dn782250.aspx and http://azure.microsoft.com/documentation/articles/documentdb-sql-query/ for syntax and examples.

See also

CreateDocumentQuery<T>(Uri, FeedOptions) CreateDocumentQuery<T>(Uri, FeedOptions)

Method to create a query for documents in the Azure Cosmos DB service.

public System.Linq.IOrderedQueryable<T> CreateDocumentQuery<T> (Uri documentCollectionUri, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = null);
abstract member CreateDocumentQuery : Uri * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IOrderedQueryable<'T>

Type Parameters

T

The type of object to query.

Parameters

documentCollectionUri
Uri Uri

The URI of the parent document collection.

feedOptions
FeedOptions FeedOptions

(Optional) The FeedOptions for processing the query results feed.

Returns

The query result set.

CreateDocumentQuery<T>(String, FeedOptions) CreateDocumentQuery<T>(String, FeedOptions)

Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB service.

public System.Linq.IOrderedQueryable<T> CreateDocumentQuery<T> (string collectionLink, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = null);
abstract member CreateDocumentQuery : string * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IOrderedQueryable<'T>

Type Parameters

T

The type of object to query.

Parameters

collectionLink
String String

The link to the parent collection resource.

feedOptions
FeedOptions FeedOptions

The options for processing the query result feed. For details, see FeedOptions

Returns

An IOrderedQueryable{T} that can evaluate the query.

Examples

This example below queries for some book documents.


public class Book 
{
    [JsonProperty("title")]
    public string Title {get; set;}
    
    public Author Author {get; set;}
    
    public int Price {get; set;}
}

public class Author
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
}
 
// Query by the Title property
Book book = client.CreateDocumentQuery<Book>(collectionLink).Where(b => b.Title == "War and Peace").AsEnumerable().FirstOrDefault();

// Query a nested property
Book otherBook = client.CreateDocumentQuery<Book>(collectionLink).Where(b => b.Author.FirstName == "Leo").AsEnumerable().FirstOrDefault();

// Perform a range query (needs an IndexType.Range on price or FeedOptions.EnableScansInQuery)
foreach (Book matchingBook in client.CreateDocumentQuery<Book>(collectionLink).Where(b => b.Price > 100))
{
    // Iterate through books
}

// Query asychronously. Optionally set FeedOptions.MaxItemCount to control page size
using (var queryable = client.CreateDocumentQuery<Book>(
    collectionLink,
    new FeedOptions { MaxItemCount = 10 })
    .Where(b => b.Title == "War and Peace")
    .AsDocumentQuery())
{
    while (queryable.HasMoreResults) 
    {
        foreach(Book b in await queryable.ExecuteNextAsync<Book>())
        {
            // Iterate through books
        }
    }
}

Remarks

The Azure Cosmos DB LINQ provider compiles LINQ to SQL statements. Refer to http://azure.microsoft.com/documentation/articles/documentdb-sql-query/#linq-to-documentdb-sql for the list of expressions supported by the Azure Cosmos DB LINQ provider. ToString() on the generated IQueryable returns the translated SQL statement. The Azure Cosmos DB provider translates JSON.NET and DataContract serialization attributes for members to their JSON property names.

See also

Applies to