DocumentQueryable.CreateDocumentQuery Method

 

Updated: April 26, 2016

Namespace:   Microsoft.Azure.Documents.Linq
Assembly:  Microsoft.Azure.Documents.Client (in Microsoft.Azure.Documents.Client.dll)

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticCreateDocumentQuery(DocumentClient, String, FeedOptions, Object)

Overloaded. This method creates a query for documents under a collection. It returns IOrderedQueryable{Document}.

System_CAPS_pubmethodSystem_CAPS_staticCreateDocumentQuery(DocumentClient, String, SqlQuerySpec, FeedOptions, Object)

Overloaded. This method creates a query for documents under a collection 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.

System_CAPS_pubmethodSystem_CAPS_staticCreateDocumentQuery(DocumentClient, String, String, FeedOptions, Object)

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

System_CAPS_pubmethodSystem_CAPS_staticCreateDocumentQuery<T>(DocumentClient, String, FeedOptions, Object)

Overloaded. This method creates a query for documents under a collection.

System_CAPS_pubmethodSystem_CAPS_staticCreateDocumentQuery<T>(DocumentClient, String, SqlQuerySpec, FeedOptions, Object)

Overloaded. This method creates a query for documents under a collection 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.

System_CAPS_pubmethodSystem_CAPS_staticCreateDocumentQuery<T>(DocumentClient, String, String, FeedOptions, Object)

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

Return to top

DocumentQueryable.CreateDocumentQuery Method (DocumentClient, String, FeedOptions, Object)

Overloaded. This method creates a query for documents under a collection. It returns IOrderedQueryable{Document}.

public static IOrderedQueryable<Document> CreateDocumentQuery(
	this DocumentClient client,
	string documentsFeedOrDatabaseLink,
	FeedOptions feedOptions = null,
	object partitionKey = null
)

Parameters

client
Type: Microsoft.Azure.Documents.Client.DocumentClient

The DocumentClient instance to execute the query.

documentsFeedOrDatabaseLink
Type: System.String

The path link for the documents under a collection, e.g. dbs/db_rid/colls/coll_rid/docs/. Alternatively, this can be a path link to the database when using an IPartitionResolver, e.g. dbs/db_rid/

feedOptions
Type: Microsoft.Azure.Documents.Client.FeedOptions

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

partitionKey
Type: System.Object

Optional partition key that can be used with an IPartitionResolver.

Return Value

Type: System.Linq.IOrderedQueryable<Document>

An IOrderedQueryable{Document} that can evaluate the query.

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.

This example below queries for documents by id.


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


Return to top

DocumentQueryable.CreateDocumentQuery Method (DocumentClient, String, SqlQuerySpec, FeedOptions, Object)

Overloaded. This method creates a query for documents under a collection 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 static IQueryable<object> CreateDocumentQuery(
	this DocumentClient client,
	string documentsFeedOrDatabaseLink,
	SqlQuerySpec querySpec,
	FeedOptions feedOptions = null,
	object partitionKey = null
)

Parameters

client
Type: Microsoft.Azure.Documents.Client.DocumentClient

The DocumentClient instance to execute the query.

documentsFeedOrDatabaseLink
Type: System.String

The path link for the documents under a collection, e.g. dbs/db_rid/colls/coll_rid/docs/. Alternatively, this can be a path link to the database when using an IPartitionResolver, e.g. dbs/db_rid/

querySpec
Type: Microsoft.Azure.Documents.SqlQuerySpec

The SqlQuerySpec instance containing the SQL expression.

feedOptions
Type: Microsoft.Azure.Documents.Client.FeedOptions

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

partitionKey
Type: System.Object

Optional partition key that can be used with an IPartitionResolver.

Return Value

Type: System.Linq.IQueryable<Object>

an IQueryable{dynamic> that can evaluate the query.

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.

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();

Return to top

DocumentQueryable.CreateDocumentQuery Method (DocumentClient, String, String, FeedOptions, Object)

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

public static IQueryable<object> CreateDocumentQuery(
	this DocumentClient client,
	string documentsFeedOrDatabaseLink,
	string sqlExpression,
	FeedOptions feedOptions = null,
	object partitionKey = null
)

Parameters

client
Type: Microsoft.Azure.Documents.Client.DocumentClient

The DocumentClient instance to execute the query.

documentsFeedOrDatabaseLink
Type: System.String

The path link for the documents under a collection, e.g. dbs/db_rid/colls/coll_rid/docs/. Alternatively, this can be a path link to the database when using an IPartitionResolver, e.g. dbs/db_rid/

sqlExpression
Type: System.String

The SQL statement.

feedOptions
Type: Microsoft.Azure.Documents.Client.FeedOptions

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

partitionKey
Type: System.Object

Optional partition key that can be used with an IPartitionResolver.

Return Value

Type: System.Linq.IQueryable<Object>

an IQueryable{dynamic> that can evaluate the query.

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.

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();

Return to top

DocumentQueryable.CreateDocumentQuery<T> Method (DocumentClient, String, FeedOptions, Object)

Overloaded. This method creates a query for documents under a collection.

public static IOrderedQueryable<T> CreateDocumentQuery<T>(
	this DocumentClient client,
	string documentsFeedOrDatabaseLink,
	FeedOptions feedOptions = null,
	object partitionKey = null
)

Parameters

client
Type: Microsoft.Azure.Documents.Client.DocumentClient

The DocumentClient instance to execute the query.

documentsFeedOrDatabaseLink
Type: System.String

The path link for the documents under a collection, e.g. dbs/db_rid/colls/coll_rid/docs/. Alternatively, this can be a path link to the database when using an IPartitionResolver, e.g. dbs/db_rid/

feedOptions
Type: Microsoft.Azure.Documents.Client.FeedOptions

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

partitionKey
Type: System.Object

Optional partition key that can be used with an IPartitionResolver.

Return Value

Type: System.Linq.IOrderedQueryable<T>

An IOrderedQueryable{T} that can evaluate the query.

Type Parameters

T

The type of object to query.

The DocumentDB 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 DocumentDB LINQ provider. ToString() on the generated IQueryable returns the translated SQL statement. The DocumentDB provider translates JSON.NET and DataContract serialization attributes for members to their JSON property names.

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
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
    }
}

Return to top

DocumentQueryable.CreateDocumentQuery<T> Method (DocumentClient, String, SqlQuerySpec, FeedOptions, Object)

Overloaded. This method creates a query for documents under a collection 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 static IQueryable<T> CreateDocumentQuery<T>(
	this DocumentClient client,
	string documentsFeedOrDatabaseLink,
	SqlQuerySpec querySpec,
	FeedOptions feedOptions = null,
	object partitionKey = null
)

Parameters

client
Type: Microsoft.Azure.Documents.Client.DocumentClient

The DocumentClient instance to execute the query.

documentsFeedOrDatabaseLink
Type: System.String

The path link for the documents under a collection, e.g. dbs/db_rid/colls/coll_rid/docs/. Alternatively, this can be a path link to the database when using an IPartitionResolver, e.g. dbs/db_rid/

querySpec
Type: Microsoft.Azure.Documents.SqlQuerySpec

The SqlQuerySpec instance containing the SQL expression.

feedOptions
Type: Microsoft.Azure.Documents.Client.FeedOptions

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

partitionKey
Type: System.Object

Optional partition key that can be used with an IPartitionResolver.

Return Value

Type: System.Linq.IQueryable<T>

An IQueryable{T} that can evaluate the query.

Type Parameters

T

The type of object to query.

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.

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" }}));

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
    }
}

Return to top

DocumentQueryable.CreateDocumentQuery<T> Method (DocumentClient, String, String, FeedOptions, Object)

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

public static IQueryable<T> CreateDocumentQuery<T>(
	this DocumentClient client,
	string documentsFeedOrDatabaseLink,
	string sqlExpression,
	FeedOptions feedOptions = null,
	object partitionKey = null
)

Parameters

client
Type: Microsoft.Azure.Documents.Client.DocumentClient

The DocumentClient instance to execute the query.

documentsFeedOrDatabaseLink
Type: System.String

The path link for the documents under a collection, e.g. dbs/db_rid/colls/coll_rid/docs/. Alternatively, this can be a path link to the database when using an IPartitionResolver, e.g. dbs/db_rid/

sqlExpression
Type: System.String

The SQL statement.

feedOptions
Type: Microsoft.Azure.Documents.Client.FeedOptions

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

partitionKey
Type: System.Object

Optional partition key that can be used with an IPartitionResolver.

Return Value

Type: System.Linq.IQueryable<T>

An IQueryable{T} that can evaluate the query.

Type Parameters

T

The type of object to query.

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.

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
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
    }
}

Return to top
Show: