I have two code examples :
var container = new CloudBlobContainer("address");
var blobs = container.ListBlobs();
foreach (var blob in blobs)
{
//Do somthing
}
and this :
var container = new CloudBlobContainer("address");
var blobs = container.ListBlobs().ToList();
foreach (var blob in blobs)
{
//Do somthing
}
Will the second example give any advantage “transaction-wise” ?
How many transaction are made to the blob storage in each example ?
Edit: It appears this question is in reference to a blog post by Martin Ingvar Kofoed Jensen. The difference between this question and that question is the call to
.Where(...).SingleOrDefault(). Because LINQ is lazy-evaluated, at the stage ofContainer.ListBlobs()it is still anIEnumerableand hasn’t yet called the REST API (no data has been retrieved). As soon as a result operation occurs (such asToList()orSingleOrDefault()), the data is downloaded. Since a non-lazy operation is called on a lazy list inside the loop in the following code, it incurs a transaction for every loop iteration:Regarding the question as it’s actually written: Both code snippets will incur a single transaction (up to 5,000 blobs). I tested both code snippets in LinqPad/Fiddler, and I only see a single API call to generate the list of blobs (replaced our storage name with {mystore}):
According to the documentation for List Blobs REST API, up to 5,000 results can be returned in a single call. So if you want to list all blobs in the entire container, it will take at most (# blobs / 5000) transactions.