DocumentQueryable.CreateDocumentQuery Method

 

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

Overload List

Name Description
System_CAPS_pubmethodSystem_CAPS_static CreateDocumentQuery(DocumentClient, String, FeedOptions, Object)

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

System_CAPS_pubmethodSystem_CAPS_static CreateDocumentQuery(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_static CreateDocumentQuery(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_static CreateDocumentQuery<T>(DocumentClient, String, FeedOptions, Object)

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

System_CAPS_pubmethodSystem_CAPS_static CreateDocumentQuery<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_static CreateDocumentQuery<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}.

See Also

DocumentQueryable Class
Microsoft.Azure.Documents.Linq Namespace

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

Syntax

public static IOrderedQueryable<Document> CreateDocumentQuery(
    this DocumentClient client,
    string documentsFeedOrDatabaseLink,
    FeedOptions feedOptions = null,
    object partitionKey = null
)
public:
[ExtensionAttribute]
static IOrderedQueryable<Document^>^ CreateDocumentQuery(
    DocumentClient^ client,
    String^ documentsFeedOrDatabaseLink,
    FeedOptions^ feedOptions = null,
    Object^ partitionKey = null
)
<ExtensionAttribute>
Public Shared Function CreateDocumentQuery (
    client As DocumentClient,
    documentsFeedOrDatabaseLink As String,
    feedOptions As FeedOptions,
    partitionKey As Object
) As IOrderedQueryable(Of Document)

Parameters

  • 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/

Return Value

Type: System.Linq.IOrderedQueryable<Document>

An IOrderedQueryable{Document} that can evaluate the query.

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.

Examples

This example below queries for documents by id.

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

See Also

Document
IDocumentQuery

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.

Syntax

public static IQueryable<object> CreateDocumentQuery(
    this DocumentClient client,
    string documentsFeedOrDatabaseLink,
    SqlQuerySpec querySpec,
    FeedOptions feedOptions = null,
    object partitionKey = null
)
public:
[ExtensionAttribute]
static IQueryable<Object^>^ CreateDocumentQuery(
    DocumentClient^ client,
    String^ documentsFeedOrDatabaseLink,
    SqlQuerySpec^ querySpec,
    FeedOptions^ feedOptions = null,
    Object^ partitionKey = null
)
<ExtensionAttribute>
Public Shared Function CreateDocumentQuery (
    client As DocumentClient,
    documentsFeedOrDatabaseLink As String,
    querySpec As SqlQuerySpec,
    feedOptions As FeedOptions,
    partitionKey As Object
) As IQueryable(Of Object)

Parameters

  • 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/

Return Value

Type: System.Linq.IQueryable<Object>

an IQueryable{dynamic> that can evaluate the query.

Remarks

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

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

See Also

Document
IDocumentQuery

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

Syntax

public static IQueryable<object> CreateDocumentQuery(
    this DocumentClient client,
    string documentsFeedOrDatabaseLink,
    string sqlExpression,
    FeedOptions feedOptions = null,
    object partitionKey = null
)
public:
[ExtensionAttribute]
static IQueryable<Object^>^ CreateDocumentQuery(
    DocumentClient^ client,
    String^ documentsFeedOrDatabaseLink,
    String^ sqlExpression,
    FeedOptions^ feedOptions = null,
    Object^ partitionKey = null
)
<ExtensionAttribute>
Public Shared Function CreateDocumentQuery (
    client As DocumentClient,
    documentsFeedOrDatabaseLink As String,
    sqlExpression As String,
    feedOptions As FeedOptions,
    partitionKey As Object
) As IQueryable(Of Object)

Parameters

  • 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/

Return Value

Type: System.Linq.IQueryable<Object>

an IQueryable{dynamic> that can evaluate the query.

Remarks

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

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

See Also

Document
IDocumentQuery

Return to top

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

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

Syntax

public static IOrderedQueryable<T> CreateDocumentQuery<T>(
    this DocumentClient client,
    string documentsFeedOrDatabaseLink,
    FeedOptions feedOptions = null,
    object partitionKey = null
)
public:
generic<typename T>
[ExtensionAttribute]
static IOrderedQueryable<T>^ CreateDocumentQuery(
    DocumentClient^ client,
    String^ documentsFeedOrDatabaseLink,
    FeedOptions^ feedOptions = null,
    Object^ partitionKey = null
)
<ExtensionAttribute>
Public Shared Function CreateDocumentQuery(Of T) (
    client As DocumentClient,
    documentsFeedOrDatabaseLink As String,
    feedOptions As FeedOptions,
    partitionKey As Object
) As IOrderedQueryable(Of T)

Parameters

  • 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/

Return Value

Type: System.Linq.IOrderedQueryable<T>

An IOrderedQueryable{T} that can evaluate the query.

Type Parameters

  • T
    The type of object to query.

Remarks

The DocumentDB LINQ provider compiles LINQ to SQL statements. Refer to https://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.

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

See Also

Document
IDocumentQuery

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.

Syntax

public static IQueryable<T> CreateDocumentQuery<T>(
    this DocumentClient client,
    string documentsFeedOrDatabaseLink,
    SqlQuerySpec querySpec,
    FeedOptions feedOptions = null,
    object partitionKey = null
)
public:
generic<typename T>
[ExtensionAttribute]
static IQueryable<T>^ CreateDocumentQuery(
    DocumentClient^ client,
    String^ documentsFeedOrDatabaseLink,
    SqlQuerySpec^ querySpec,
    FeedOptions^ feedOptions = null,
    Object^ partitionKey = null
)
<ExtensionAttribute>
Public Shared Function CreateDocumentQuery(Of T) (
    client As DocumentClient,
    documentsFeedOrDatabaseLink As String,
    querySpec As SqlQuerySpec,
    feedOptions As FeedOptions,
    partitionKey As Object
) As IQueryable(Of T)

Parameters

  • 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/

Return Value

Type: System.Linq.IQueryable<T>

An IQueryable{T} that can evaluate the query.

Type Parameters

  • T
    The type of object to query.

Remarks

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

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

See Also

Document
IDocumentQuery

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

Syntax

public static IQueryable<T> CreateDocumentQuery<T>(
    this DocumentClient client,
    string documentsFeedOrDatabaseLink,
    string sqlExpression,
    FeedOptions feedOptions = null,
    object partitionKey = null
)
public:
generic<typename T>
[ExtensionAttribute]
static IQueryable<T>^ CreateDocumentQuery(
    DocumentClient^ client,
    String^ documentsFeedOrDatabaseLink,
    String^ sqlExpression,
    FeedOptions^ feedOptions = null,
    Object^ partitionKey = null
)
<ExtensionAttribute>
Public Shared Function CreateDocumentQuery(Of T) (
    client As DocumentClient,
    documentsFeedOrDatabaseLink As String,
    sqlExpression As String,
    feedOptions As FeedOptions,
    partitionKey As Object
) As IQueryable(Of T)

Parameters

  • 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/

Return Value

Type: System.Linq.IQueryable<T>

An IQueryable{T} that can evaluate the query.

Type Parameters

  • T
    The type of object to query.

Remarks

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

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

See Also

Document
IDocumentQuery

Return to top