Tous les produits
Search
Centre de documentation

Tablestore:Filters

Dernière mise à jour :Sep 23, 2026

Les filtres Tablestore permettent d'affiner les résultats de requête côté serveur selon des conditions spécifiées. Cette rubrique décrit l'utilisation des filtres avec le SDK .NET.

Prérequis

Initialiser un client Tablestore

Types de filtre

Tablestore propose deux types de filtres.

  • RelationalCondition : vérifie si la valeur d'une colonne d'attribut unique satisfait à une condition donnée.

  • CompositeCondition : combine plusieurs conditions pour filtrer les données.

RelationalCondition

public class RelationalCondition : IColumnCondition

Paramètres

Paramètre

Type

Description

Operator (obligatoire)

CompareOperator

L'opérateur relationnel. Les opérateurs pris en charge sont EQUAL, NOT_EQUAL, GREATER_THAN, GREATER_EQUAL, LESS_THAN et LESS_EQUAL.

ColumnName (obligatoire)

string

Nom de la colonne d'attribut à évaluer.

ColumnValue (obligatoire)

ColumnValue

Valeur de comparaison.

PassIfMissing (facultatif)

bool

Indique s'il faut renvoyer une ligne lorsque la colonne d'attribut spécifiée est absente. La valeur par défaut true renvoie la ligne.

LatestVersionsOnly (facultatif)

bool

Spécifie s'il faut évaluer uniquement la dernière version des données d'une colonne d'attribut, même si plusieurs versions existent. La valeur par défaut est true.

Exemple

Cet exemple exécute une requête de plage sur les données dont la clé primaire se situe dans l'intervalle [row1, row3). Il applique ensuite un filtre pour ne renvoyer que les lignes où la colonne d'attribut col1 a la valeur val1.

try
{
    // Set the start primary key for the query.
    PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
    {
        { "id", new ColumnValue("row1") }
    };
    // Set the end primary key for the query. The end key is exclusive.
    PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
    {
        { "id", new ColumnValue("row3") }
    };
    // Create a filter with the condition: col1 == "val1".
    RelationalCondition relationalCondition = new RelationalCondition("col1", CompareOperator.EQUAL, new ColumnValue("val1"));

    // Call the GetRange method to read row data.
    GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, null, null, relationalCondition);
    GetRangeResponse getRangeResponse = client.GetRange(getRangeRequest);

    // Process the response.
    Console.WriteLine($"RequestId: {getRangeResponse.RequestID}");
    Console.WriteLine($"Read CU Cost: {getRangeResponse.ConsumedCapacityUnit.Read}");
    Console.WriteLine($"Write CU Cost: {getRangeResponse.ConsumedCapacityUnit.Write}");
    Console.WriteLine("Row Data: ");
    foreach (Row row in getRangeResponse.RowDataList)
    {
        Console.WriteLine(row);
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Get Range failed, exception: {ex.Message}");
}
  • Pour exclure une ligne dont la colonne d'attribut spécifiée est absente, définissez PassIfMissing sur false.

    relationalCondition.PassIfMissing = false;

CompositeCondition

Vous pouvez combiner jusqu'à 32 conditions.

public class CompositeCondition : IColumnCondition

Paramètres

Paramètre

Type

Description

LogicOperator (obligatoire)

LogicOperator

L'opérateur logique. Les opérateurs pris en charge sont NOT, AND et OR.

subConditions (obligatoire)

List<IColumnCondition>

Liste de filtres à combiner via l'opérateur logique. Elle peut contenir des objets RelationalCondition et CompositeCondition.

Exemple

Cet exemple illustre une requête de plage sur les données dont la clé primaire se situe dans l'intervalle [row1, row3). Il utilise ensuite une CompositeCondition pour filtrer les résultats.

try
{
    // Set the start primary key for the query.
    PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
    {
        { "id", new ColumnValue("row1") }
    };
    // Set the end primary key for the query. The end key is exclusive.
    PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
    {
        { "id", new ColumnValue("row3") }
    };

    // Create RelationalCondition 1: col1 == "val1".
    RelationalCondition relationalCondition1 = new RelationalCondition("col1", CompareOperator.EQUAL, new ColumnValue("val1"));
    // Create RelationalCondition 2: col2 == "val2".
    RelationalCondition relationalCondition2 = new RelationalCondition("col2", CompareOperator.EQUAL, new ColumnValue("val2"));
    // Create CompositeCondition 1: col1 == "val1" OR col2 == "val2".
    CompositeCondition compositeCondition1 = new CompositeCondition(LogicOperator.OR);
    compositeCondition1.AddCondition(relationalCondition1);
    compositeCondition1.AddCondition(relationalCondition2);
    // Create RelationalCondition 3: col3 == "val3".
    RelationalCondition relationalCondition3 = new RelationalCondition("col3", CompareOperator.EQUAL, new ColumnValue("val3"));
    // Create CompositeCondition 2, which combines the previous conditions: (col1 == "val1" OR col2 == "val2") AND col3 == "val3".
    CompositeCondition compositeCondition2 = new CompositeCondition(LogicOperator.AND);
    compositeCondition2.AddCondition(compositeCondition1);
    compositeCondition2.AddCondition(relationalCondition3);

    // Call the GetRange method to read row data.
    GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, null, null, compositeCondition2);
    GetRangeResponse getRangeResponse = client.GetRange(getRangeRequest);

    // Process the response.
    Console.WriteLine($"RequestId: {getRangeResponse.RequestID}");
    Console.WriteLine($"Read CU Cost: {getRangeResponse.ConsumedCapacityUnit.Read}");
    Console.WriteLine($"Write CU Cost: {getRangeResponse.ConsumedCapacityUnit.Write}");
    Console.WriteLine("Row Data: ");
    foreach (Row row in getRangeResponse.RowDataList)
    {
        Console.WriteLine(row);
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Get Range failed, exception: {ex.Message}");
}

Références