Creating object hierarchies with LINQ

I’m constantly amazed by the power and simplicity of LINQ.  I love that I can turn a flat table into a  object graph with a couple of view models and a single LINQ query.

Given a repository method that returns a list of Tracks based on the following SQL:

Select Id, Title, Artist, Album, Year, TrackNumber, FilePathName From Track Order By Artist, Album, TrackNumber, Title

We can create a nice hierarchy of  Artists, each with 0 or more albums, of which each may have a number of tracks – with a nice, neat LINQ query:

var artists = _libraryRepository.FetchLibrary()
    .AsParallel()
    .GroupBy(track => track.Artist)
    .Select(group => new Artist(group.Key,
        group.GroupBy(track2 => track2.Album)
            .Select(group2 => new Album(group2.Key,group2)))
   );

And the models:

public class Artist
{
    public string Name { get; set; }
    public IEnumerable<Album> Albums { get; private set; }

    public Artist() { }

    public Artist(string name, IEnumerable<Album> albums)
    {
        Name = name;
        Albums = albums;
    }
}

public class Album
{
    public string Name { get; set; }
    public IEnumerable<Track> Tracks { get; private set; }

    public Album() { }

    public Album(string name, IEnumerabl<Track> tracks)
    {
        Name = name;
        Tracks = tracks;
    }
}

public class Track
{
    public Guid Id { get; private set;}
    public string Title { get; set; }
    public string Artist { get; set; }
    public string Album { get; set; }
    public string FilePathName { get; set; }
    public int? Year { get; set; }
    public int? TrackNumber { get; set; }

    public Track() {}

    public Track(Guid id, string filePathName)
    {
        Id = id;
        FilePathName = filePathName;
    }
}

Got comments?  Tweet me @padgettrowell

On switching from Apple to Android

My thoughts on having recently switched from an Apple iPhone 4 to an Android Samsung Galaxy S3:

  • Being able to plug my Android into any computer, drag and drop media files into the relevant directories then be able to immediately access that content on the phone via the music/video player apps is absolutely freaking wonderful. Good bye iTunes.
  • I miss the visual consistency, simplicity and elegance of the iPhone apps.  By contrast, Android apps seem to favour quantity of features and breadth of configurable options over making simple software that *just works*.
  • I’ve found the IntelliJ / Android development process far simpler  and more intuitive  than the equivalent  iOS / Xcode.  It’s also a lot cheaper, especially when you factor in the Mac Tax.
  • Not being able to uninstall the default ‘system’ (bloatware) apps installed by Telstra and Samsung is ridiculous.  I hope they both address this BEFORE getting sued.
  • The 4.8-inch Super AMOLED HD screen is superb – it’s a real pleasure to watch video content on.
  • Turn-by-turn navigation and offline maps is great – epically the later.  As a motorcycle rider who likes to get  outside of cell service areas, I really appreciate this feature.
  • The default messaging and email apps on the S3 suck.  Still looking for reasonable replacements.
  • I miss Reeder.

If I was to summarise my experience after one week: Being untethered from iTunes is undeniably the biggest benefit of moving to Android.  It’s archaic that users are forced to rely on a desktop application to manage their device for them – especially given the general ‘everything in the cloud’ strategy that’s occurring right now.  Also, Androids open and accessible approach to application development is both it’s greatest strength, and it’s greatest weakness.