Azure table storage using LINQ C# | Blog | George Harrison-Webb | Software Developer

Azure table storage using LINQ C#

Author: George Harrison-Webb


There are few resources out there giving a simple method of CRUD operations for Azure table storage. I am going to be showing you a very simple and easy way to read from a table.

An exisiting Azure table storage account
At least 1 table with data
Basic C# knowledge

First you will need to use the NuGet package manage to install 'Microsoft.Azure.Storage'
You will need to add this reference to a new class
Next, we will setup the table authentication. To make this more flexible we will take the table name from when it is called.

private static CloudTable TableAuthentication (string tableName) { try { string accountName = "Your table storage account name"; string key = "123456789"; StorageCredentials cred = new StorageCredentials(accountName, key); CloudStorageAccount storageAcc = new CloudStorageAccount(cred, useHttps: true); CloudTable table = client.GetTableReference(tableName); return table; } catch { return null; } }
We will seperate the actual query into another method. I'm going to call it seperatly for more control. Here I will be using the LINQ to SQL query to return the results into an object

private static void FetchResults(CloudTable table) { var linqQuery = table.CreateQuery<YourTableEntity>().Where(a => a.TableColumn == "123").FirstOrDefault(); //show your result Console.Write(linqQuery.TableColum2); }
You will of course, be required to create a table entity for the table/s you wish to query. We can do that as below

public class CustomerEntity : TableEntity { public string TableColumn { get; set; } public string TableColumn2 { get; set; } }
Now we have built all the required methods, we just need to call them. Here we can also name the table we are trying to call

public class SomeEventTrigger() { string tableName = "myTableName"; var table = TableAuthentication(tableName); FetchResults(table); }
And there we have it, a nice simple read operation from Azure storage table using LINQ. You should note that it is not possible to join tables, nor would it make sense to. To make joins you will need to do seperate queries

Category: How to guide

Azure table storage
How to