Search code examples
c#azureazure-storageazure-table-storage

Unable to use string-based methods on a string property inside an Azure storage table query lambda


While querying an Azure storage table I get an exception.

Here is my query:

tableClient.QueryAsync<Resource>(resource => resource.PartitionKey == projectId.ToString()
            && String.Equals(resource.FullName, fullName, StringComparison.InvariantCultureIgnoreCase));

resource.FullName and fullName are both of type string.

But this incurs the exception...

Incorrect number of arguments supplied for call to method 'Boolean Equals(System.String, System.String, System.StringComparison)' (Parameter 'method')

And similarly, if I add an additional condition (before the String.Equals)...

&& !String.IsNullOrWhiteSpace(resource.FullName)

... this incurs the exception...

Method IsNullOrWhiteSpace not supported.

If I change the string comparison to...

resource.FullName.ToLower() == fullName.ToLower()

... then I'm told that ToLower is not supported.

What's going on here? Why can't I call String.Equals or String.IsNullOrWhiteSpace on a property which is of type string? Am I misunderstanding something, and resource.FullName is actually not a string?


Solution

  • Please follow the below suggestions and check if that helps.

    1. Please check by updating String.Equals to String.Compare?
    2. And instead of using ToLower() method, you can try to use String.Compare method with StringComparison.OrdinalIgnoreCase option to achieve case-insensitive comparison:
    tableClient.QueryAsync<Resource>(resource => 
        resource.PartitionKey == projectId.ToString() &&
        String.Compare(resource.FullName, fullName, StringComparison.OrdinalIgnoreCase) == 0);
    
    1. You can also use String.IsNullOrEmpty method along with String.Trim method:
    tableClient.QueryAsync<Resource>(resource => 
        resource.PartitionKey == projectId.ToString() &&
        !String.IsNullOrEmpty(resource.FullName) &&
        !String.IsNullOrEmpty(resource.FullName.Trim()));
    

    Hope this helps.