I’ve been playing around with a Chrome/ASP.NET 2.0 based website based on the flickr API on my personal time, and one of the things that has unexpectedly come in really handy has been LINQ.
Flickr – which for those of you that don’t know it – is a fast-growing community for sharing and publishing photos, ad provides a pretty extensive API to access its database from custom tools and websits. a cool .NET wrapper is available for this API in form of the open source Flickr.NET project.
The Flickr.NET assembly encapsulates the acess to flickr in a set of nice and easy-to-use classes that feel natural to .NET developers from the get-go, making it easy to access and represent data from flickr quickly and with just a few lines of code.
For example, listing all of a user’s public photos is as simple as
var lPhotos := Flickr.PeopleGetPublicPhotos(USERID); for each lPhoto in lPhotos.PhotoCollection do Response.Write(String.Format('', lPhotoUrl, lPhoto.Title)]));
Flickr.NET exposes a lot of collections that contain data – lists of photos, lists of photo sets, lists of contacts, lists of favorites, etc. The one downside is that many of these collections are unsorted (or not sorted the way you want them to be), and don’t provide methods to easily sort them or access them in an ordered fashion.
LINQ to the rescue!
But fear not, this is where LINQ comes in. For example, let’s assume that from an unsorted list of contacts, we want to show all contacs – not only sorted, but also group by status (family, friends and mere contacts). Nothing simpler then that: just use the GroupBy and OrderBy extension methods on the contact list, and you’re done:
var lContacts := Flickr.ContactsGetList(); for each g in lContacts.ContactCollection.GroupBy(c -> c.IsFriend+c.IsFamily)do begin*case* g.Key of 0:Response.Write('Contacts'); 1:Response.Write('Friends'); 2:Response.Write('Family'); end; for each c in g.Group.OrderBy(c -> c.UserName)do Response.Write(... c.UserName ...); end;
Note the two parts of the code marked in italic: the GroupBy extension method patitions the collection in several sub-collections, based on a particlar key. In out case, we use the sum of IdFriend and IsFamily (both of which are 0/1 integers indicating if the contact is either a friend or a family member). This gives us a collection of three items, each conatining a separate list of Conacts (Family, Friends, and ormal Contacts, respectively.
Secondly, we apply the the OrderBy extension method to each of these sub-lists, ordering them by the UserName.
Note that these are not methods provided by the Contacts collection classesof Flickr.NET – they are extension methods provided by LINQ, applicable to any IEnumerable.
As you can see, two collection operations that would normally have taken a considerable amount of plumbing code are made as easy as a simple method call, with LINQ, Extension Methods and Lambda Expressions.
And in case you’re wondering: the above is not mock-up or proof-of-concept code, but comes from a real and live website, using the LINQ May Preview and an early alpha version of Joyride, the next major version of our Chrome compiler.