none
Best Way to Check if Entity Exists

    Question

  • From @Chronos via Twitter:

    "what's the proper way to check if an entity exists in a table if it possibly doesn't exist? it doesn't feel right to use a try/catch instead of an if/else block here, is there some safer way to do this?"

    Thanks,

    @AzureSupport

    Thursday, March 3, 2016 8:28 PM

Answers

  • You could do a TableOperation.Retrieve on the entity you are looking for. If the result is null, then it doesn't exist.

    See an example here:

    https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-tables/#insert-or-replace-an-entity

    Friday, March 11, 2016 4:07 AM

All replies

  • by entity are we talking about linq?
    Thursday, March 3, 2016 8:38 PM
  • I'm trying to use Azure Table Storage here. I want to search for an entity/row in the table, but that row may not exist. Currently the code shown in all examples assumes it will exist.

    If it doesn't exist, it throws an exception, which forces me to use a try/catch for flow control instead of an if/else on the result object which should really just have it's .Result field as null.

    I haven't found an example that shows a real hardened use case for searching something up and dealing with potential failure cases.

    Thursday, March 3, 2016 9:13 PM
  • You could do a TableOperation.Retrieve on the entity you are looking for. If the result is null, then it doesn't exist.

    See an example here:

    https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-tables/#insert-or-replace-an-entity

    Friday, March 11, 2016 4:07 AM