Share via


Container.GetItemLinqQueryable<T> Method

Definition

This method creates a LINQ query for items under a container in an Azure Cosmos DB service. IQueryable extension method ToFeedIterator() should be use for asynchronous execution with FeedIterator, please refer to example 2.

public abstract System.Linq.IOrderedQueryable<T> GetItemLinqQueryable<T> (bool allowSynchronousQueryExecution = false, string continuationToken = default, Microsoft.Azure.Cosmos.QueryRequestOptions requestOptions = default, Microsoft.Azure.Cosmos.CosmosLinqSerializerOptions linqSerializerOptions = default);
abstract member GetItemLinqQueryable : bool * string * Microsoft.Azure.Cosmos.QueryRequestOptions * Microsoft.Azure.Cosmos.CosmosLinqSerializerOptions -> System.Linq.IOrderedQueryable<'T>
Public MustOverride Function GetItemLinqQueryable(Of T) (Optional allowSynchronousQueryExecution As Boolean = false, Optional continuationToken As String = Nothing, Optional requestOptions As QueryRequestOptions = Nothing, Optional linqSerializerOptions As CosmosLinqSerializerOptions = Nothing) As IOrderedQueryable(Of T)

Type Parameters

T

The type of object to query.

Parameters

allowSynchronousQueryExecution
Boolean

(Optional)the option which allows the query to be executed synchronously via IOrderedQueryable.

continuationToken
String

(Optional) The continuation token in the Azure Cosmos DB service.

requestOptions
QueryRequestOptions

(Optional) The options for the item query request.

linqSerializerOptions
CosmosLinqSerializerOptions

(Optional) The options to configure Linq Serializer Properties. This overrides properties in CosmosSerializerOptions while creating client

Returns

(Optional) An IOrderedQueryable{T} that can evaluate the query.

Examples

  1. This example below shows LINQ query generation and blocked execution.
public class Book 
{
    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 = container.GetItemLinqQueryable<Book>(true)
                     .Where(b => b.Title == "War and Peace")
                     .AsEnumerable()
                     .FirstOrDefault();

// Query a nested property
Book otherBook = container.GetItemLinqQueryable<Book>(true)
                          .Where(b => b.Author.FirstName == "Leo")
                          .AsEnumerable()
                          .FirstOrDefault();

// Perform iteration on books
foreach (Book matchingBook in container.GetItemLinqQueryable<Book>(true)
                           .Where(b => b.Price > 100))
{
    // Iterate through books
}
  1. This example below shows LINQ query generation and asynchronous execution with FeedIterator.

// LINQ query generation
using (FeedIterator<Book> setIterator = container.GetItemLinqQueryable<Book>()
                     .Where(b => b.Title == "War and Peace")
                     .ToFeedIterator())
{                   
    //Asynchronous query execution
    while (setIterator.HasMoreResults)
    {
        foreach(var item in await setIterator.ReadNextAsync())
        {
            Console.WriteLine(item.Price); 
        }
    }
}

Remarks

LINQ execution is synchronous which will cause issues related to blocking calls. It is recommended to always use ToFeedIterator(), and to do the asynchronous execution.

Applies to

See also