by Jon Sagara
June 4, 2009 at 3:32 PM
Here's another example of how I feel LINQ improves the readability of my code. This is the before version, using standard looping to populate a list of objects:
private IEnumerable<Album> ReadAlbums(LLAlbum[] llAlbums)
{
Album album;
IList<Album> albums = new List<Album>(llAlbums.Length);
foreach (LLAlbum llAlbum in llAlbums)
{
album = new Album()
{
AlbumId = llAlbum.Id
};
albums.Add(album);
}
return albums;
}
And here is the after version, using a LINQ query:
private IEnumerable<Album> ReadAlbums(LLAlbum[] llAlbums)
{
return from llAlbum in llAlbums
select new Album
{
AlbumId = llAlbum.Id
};
}
Now, depending on your familiarity with LINQ, you may or may not agree with me, but I love it!