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
- LINQPad (https://www.linqpad.net/)
- Azure subscription
- Storage account (Table)
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.
2. Right click TABLES and add new table.
3. Add some data. Or we can use Azure Storage Explorer to upload data from local computer.
4. I added people data from swapi
Connect to the table from LINQPad
As data is ready, let's connect and query data.
1. Open LINQPad and click Add connection.
2. Click View more drivers....
3. Click Install to add the driver, then close. Click Accept if you are prompted to install modules.
4. Select installed driver and click Next.
5. Enter name and storage account key, then click OK.
6. Select connected data source from Connection dropdown.
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}
Filter data by a column
from c in LINQPadDemoTable
where c.birth_year == "19BBY"
select new { c.name, c.birth_year}
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})
Parse Json string
We can also use power of NuGet for more complex scenario. The vechies column contains json array string like below.
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) })
2. As it cannot resolve JArray, it shows in red. Right click the pain and select Nuget Package Manager.
3. Click Add to Query button for Json.NET.
4. Once added, click Add namespace link.
5. Now we can execute the query.
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 })
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.
Top comments (1)
Very helpful article, thank you so much