Search code examples
c#linqazure-table-storageazure-tablequery

Running a Contains operator in Azure Table Service linq query


I'd like to know why/how this query is running on a Azure Storage table, given that 'contains' are not allowed in Azure Table Service? Is this not doing what I think it's doing? It's running and fetching values. Also, is this fetching the whole table first then filtering? In the debugger it looks like it doesn't run fully until I run the ToList()?

Here is my code, the bottom line I use a contains.

List<string> partitionIds = new List<string> {"1", "2", "3"};

var table = // get table here...

var result = table.ExecuteQuery(new TableQuery<ConnectionEntity>()); 
var queryResult = result.Where(w => partitionIds.Contains(w.PartitionKey)).ToList();

Solution

  • As stated on the site you provided, Azure Table service does not support the validation of a contain statement. As the data is saved in a no-SQL environment, a contains statement could require enormous amounts of power, depending on the size of your data set. At the moment, your query sends a request to the server that asks for the whole data set (result.GetAll() ). On your system, it evaluates the contains part on the data set that was returned by the server (result.where(contains).tolist() ). This way, the server doesn't evaluate your contains statement, so the server is satisfied. However, your server still needs to do a lot of work to evaluate this statement!

    For your second question: The standard way of getting data in Entity Framework, is getting it just in time. This means, it only evaluates the query the moment the data is needed, which is at the moment the data is transformed to a list, when you try to loop over it, or when you try to print it. The only way to get it earlier, is by explicitly loading it by calling result.Load() instead of .toList(). After that, you can still call .toList(), but the result set was loaded at the moment you explicitly stated .Load().