共用方式為


UserDefinedFunctionProperties Class

Definition

Represents a user defined function in the Azure Cosmos service.

public class UserDefinedFunctionProperties
type UserDefinedFunctionProperties = class
Public Class UserDefinedFunctionProperties
Inheritance
UserDefinedFunctionProperties

Examples

The following examples show how to register and use UDFs.

await this.container.UserDefinedFunctions.CreateUserDefinedFunctionAsync(
    new UserDefinedFunctionProperties 
    { 
        Id = "calculateTax", 
        Body = @"function(amt) { return amt * 0.05; }" 
    });

QueryDefinition sqlQuery = new QueryDefinition(
    "SELECT VALUE udf.calculateTax(t.cost) FROM toDoActivity t where t.cost > @expensive and t.status = @status")
    .WithParameter("@expensive", 9000)
    .WithParameter("@status", "Done");

using (FeedIterator<double> feedIterator = this.container.Items.GetItemsQueryIterator<double>(
    sqlQueryDefinition: sqlQuery,
    partitionKey: "Done"))
{
    while (feedIterator.HasMoreResults)
    {
        foreach (var tax in await feedIterator.ReadNextAsync())
        {
            Console.WriteLine(tax);
        }
    }
}

Remarks

Azure Cosmos supports JavaScript user defined functions (UDFs) which are stored in the database and can be used inside queries. Refer to https://docs.microsoft.com/azure/cosmos-db/sql-api-sql-query#javascript-integration for how to use UDFs within queries. Refer to https://docs.microsoft.com/azure/cosmos-db/programming#udf for more details about implementing UDFs in JavaScript.

Constructors

UserDefinedFunctionProperties()

Properties

Body

Gets or sets the body of the user defined function for the Azure Cosmos DB service.

ETag

Gets the entity tag associated with the resource from the Azure Cosmos DB service.

Id

Gets or sets the Id of the resource in the Azure Cosmos DB service.

SelfLink

Gets the self-link associated with the resource from the Azure Cosmos DB service.

Applies to