**Update** I was corrected that FindAll and Find were available since 2.0 not 3.5. Apologies. :)
If you’re using .NET 3.5, you’ll find two new methods on the generic List<T> class. These are the FindAll and Find methods, both taking a Predicate<T> as a parameter which provides the matching functionality for returning the correct item/s. Find will return the first item that the predicate matches while FindAll<T> returns a List<T> containing each item that matches the predicate.
So, why avoid the method? The answer lies in the implementation. The FindAll method internally creates a new List<T>, loops through each item in the list, does the matching against the predicate and if the match returns true, adds the current item to the list. Finally the method returns the newly created List. Why is this a bad idea? Because of the creation of that new List<T> internally which if I use this in further expressions, it becomes increasingly inefficient. For example:
List<string> myStrings = new List<string> { “Hello”, “ray”, “AAA”, “BBB”, “aa”, “aaaaa”};
var query = myStrings.FindAll(s => s.Contains(“a”)).FindAll(s => s.Length == 2);
This query will enumerate myStrings, returning 3 items and then enumerate that new list using the Length test returning a new List containing 1 item. This means that we enumerate 2 Lists and end up creating 2 new Lists. If I instead wrote this query in this fashion:
List<string> strings = new List<string> { “Hello”, “ray”, “King”, “AAA”, “BBB”, “aa”, “aaaaa”}; var query = strings.Where(s => s.Contains(“a”)).Where(s => s.Length == 2);
We now find that the collection is enumerated only once. Instead of FindAll using the yield statement which would allow for much more composable queries, we’re left with an extremely inefficient method of finding items in our lists.
Somewhat ironic after my previous post on investing in Azure, but I have an Azure token to give away. This gives you:
- Total compute usage: 2000 VM hours
- Cloud storage capacity: 50GB
- Total storage bandwidth: 20GB/day
This first person to drop me an email at ray@vistasquad.co.uk or send me a message on my twitter account will have the code!
I’ve spent the last week involved in porting a Silverlight application from an IIS hosted solution to a completely Azure hosted solution. The application is quite complex, currently with a SQL Server backend, all data communicated via WCF services to the Silverlight application. Authentication is done via LiveID with a authorisation done in Silverlight.
Moving the data to Azure table storage was done in 2 days with the Silverlight app currently on hold as Azure has had some issues starting our web roles.
This got me thinking. With all the technology currently released or about to be released, should I be spending my time getting involved with Azure? With Silverlight 3 most probably being announced at Mix09, ASP.NET MVC about to go RTM and .NET 4.0 with the parallel extensions available to play around with, what is the best for my career?
What is the potential for you getting into a large Azure deployment compared to Silverlight, or ASP.NET MVC? I’m guessing more likely to be the latter two over the cloud. Table Storage, Queues, Blob storage, Azure SQL Data services – all of these make for a very large knowledge base that you require if you’re going to “get” Azure. What will you choose?