DEV Community

loading...

Query Azure Table Storage with LINQPad6

kenakamu profile image Kenichiro Nakamura ・3 min read

LINQPad is a world famous LINQ/C# playground which you can install locally (for Windows). There are bunch of out-of-box connectors for various services, including major cloud services, and 3rd party connectors which let you connect many data sources.

One of the connector I try this time is Azure Table storage and I show you how we can query table data.

Prerequisites

Azure Table Storage

Azure Table Storage is a key/value storage which we can store data easily.

Let's create a table and add sample data.

1. Go to Azure Portal and create a storage account, then go to storage explorer.

Alt Text

2. Right click TABLES and add new table.

Alt Text

3. Add some data. Or we can use Azure Storage Explorer to upload data from local computer.

4. I added people data from swapi

Alt Text

Connect to the table from LINQPad

As data is ready, let's connect and query data.

1. Open LINQPad and click Add connection.

Alt Text

2. Click View more drivers....

Alt Text

3. Click Install to add the driver, then close. Click Accept if you are prompted to install modules.

Alt Text

4. Select installed driver and click Next.

Alt Text

5. Enter name and storage account key, then click OK.

Alt Text

6. Select connected data source from Connection dropdown.

Alt Text

That's all.

Query data via LINQ

As name infers, LINQPad lets you query data by using LINQ. Let's try some.

Query a couple of columns from the table.

from c in LINQPadDemoTable select new { c.name, c.birth_year}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Filter data by a column

from c in LINQPadDemoTable 
where c.birth_year == "19BBY" 
select new { c.name, c.birth_year} 
Enter fullscreen mode Exit fullscreen mode

Alt Text

Query in C# way

We can also use C# expression.

Query by using .WHERE .SELECT

LINQPadDemoTable
    .Where(x => x.birth_year == "19BBY")
    .Select(x => new { x.name, x.birth_year})
Enter fullscreen mode Exit fullscreen mode

Alt Text

Parse Json string

We can also use power of NuGet for more complex scenario. The vechies column contains json array string like below.

Alt Text

We can use Json.NET C# library to parse it.

1. Enter following expression.

LINQPadDemoTable
    .Where(x => x.birth_year == "19BBY")
    .Select(x => new { x.name, x.birth_year, vehicles = JArray.Parse(x.vehicles) })
Enter fullscreen mode Exit fullscreen mode

2. As it cannot resolve JArray, it shows in red. Right click the pain and select Nuget Package Manager.

image

3. Click Add to Query button for Json.NET.

image

4. Once added, click Add namespace link.

image

5. Now we can execute the query.

image

6. Let's further digging into array object. Convert the results to List and query by SelectMany.

LINQPadDemoTable
    .Where(x => x.birth_year == "19BBY").ToList()
    .Select(x => new { x.name, vehicles = JArray.Parse(x.vehicles) })
    .SelectMany(x => x.vehicles, (x, vehicle) => new { x.name, vehicle })
Enter fullscreen mode Exit fullscreen mode

image

C# statement/program

LINQPad let you write C# program as well, so if you are interested in this tool, please download and play with it.

Discussion (0)

pic
Editor guide