LINQ, Dare, IanG and me

by Matt 4. January 2008 11:31

I've been wanting to have a play around with LINQ for quite a while now. I had a good look when VS2008 RTM'd and I was very impressed. It's a very elegant design, building slowly but surely on the building blocks of type inference, anonymous types, anonymous delegates and lambdas, extension methods and of course the master stroke of deferred execution via iterators. Clearly more than the sum of its parts. Mike Taulty has a great post that details how it's all composed. (And that's just LINQ to objects. The pluggable providers of IQueryable are a whole new ball game.)

But I hadn't got my hands dirty until today.

Too bad Ian Griffiths stole my thunder. I was looking at exactly the same problem as he's just posted about - Dare Obasanjo lamenting that anonymous types won't give him the same effect as tuples. Ian has (of course) nailed anything I wanted to say on the matter, so go read his post; anonymous types are not equivalent to tuples, and if you want to use them to acheive similar results, you need to restructure your code. And use LINQ.

In fact, just looking at the code makes you want to run for LINQ. It's a prime candidate. It's got several loops over several different collections. Each loop filters or maps a previous sequence to produce a new sequence, and the final loop is then iterated for display. That's exactly what LINQ is designed for.

And for a first attempt, I'm very pleased with how mine turned out. At the time his post dropped into my feed reader, here's what I had:

// Get a sequence of appropriate items
var items = from fileInfo in new DirectoryInfo(cache_location).GetFiles("*.xml")
            let doc = XElement.Load(fileInfo.FullName)
            let feedTitle = (string)doc.Element("Title") ?? string.Empty
            from rssItem in
                (from itemNode in doc.Descendants("Items")
                 where !bool.Parse((string)itemNode.Element("IsDeleted") ?? "False")
                     && !bool.Parse((string)itemNode.Element("IsErrorMessage") ?? "False")
                 select MakeRssItem(itemNode))
            where rssItem.OutgoingLinks.Count > 0
            where filterFunc(rssItem)
            select new
            {
                Item = rssItem,
                FeedTitle = feedTitle
            };

// Map the appropriate items to a list of all outgoing links with a chain of votes
var linksWithVotes = from item in items
                     from outgoingLink in item.Item.OutgoingLinks
                     group new
                     {
                         Item = item.Item,
                         FeedTitle = item.FeedTitle,
                         Weight = voteFunc(item.Item)
                     } by outgoingLink.Key;

// Collapse the groups down to a list of links with scores
var weightedLinks = (from linkWithVote in linksWithVotes
                    select new
                    {
                        Url = linkWithVote.Key,
                        Weight = linkWithVote.Sum(x => x.Weight),
                        Votes = linkWithVote
                    }).OrderByDescending(x => x.Weight);

foreach (var item in weightedLinks.Take(10))
	...

Now, I've got more than Ian does. That's because I was working from Dare's previous post about building a meme tracker for RSS Bandit in C# 3.0, which has the full program. So my first LINQ statement loads and transforms the rss items (modified to pull the items from my Sharpreader cache, not RSS Bandit). There are a couple of other points where I can learn from Ian's solution, mainly the use of the "into" clause.

I create "linksWithVotes" (Ian and Dare's "all_links") by finishing off with a group, meaning I have to deal with a group in my next query. Ian pushes the group "into" a variable, and then pulls the votes out of that in the final select, giving a much nicer final shape (a flat sequence).

Similarly, I called the OrderByDescending extension method directly. Ian gets it into the natural query by using another "into" and following it up with simple select.

And finally, I wasn't taking the minimum weight of the votes per feed title (Ian's a little confused about this one. I'm not massively sure on the reasoning, but I think it's so that if I always link to a particular url, only my oldest, weakest vote counts. I think it beats gaming, but I could be wrong.) And that's also solved by grouping "into" a variable and then selecting the min value, and summing that sequence.

I'm incredibly impressed that this whole set of loops can be reduced to a single foreach statement, but like Ian, I'm a bit unsure on the readability of this solution (mine or Ian's). As Ian says, this could be because of the nature of the algorithm, but I also wonder if it's partly the new-ness of the LINQ syntax, and having to mentally translate it into iterators and extension methods. Testing also looks tricky with this. But from a geeky-cool-new-toy perspective it's brilliant. It's so much more declarative - I've only got one loop, and that's over the 10 result items I'm interested in - I'm not even looping over the files to read them in.

To quote Ian's post:

"In summary, although I’m still finding my feet, I’m rather coming to like LINQ."

Tags:

Comments (14) -

suv review
suv review
7/20/2011 10:32:33 PM #

Hi, just required you to know I he added your website to my Google bookmarks due to your layout. But seriously, I consider your net web-site has 1 in the freshest theme I??ve came across. It extremely helps make studying your blogging site significantly easier.

Reply

best hybrid cars 2011
best hybrid cars 2011
7/22/2011 4:15:18 AM #

Una pagina sarà dedicata agli accessori, una alle giacche e ai giubbotti. Troverai le indicazioni per lo spaccio o negozio Moncler più vicino a casa tua e tutte le offerte più vantaggiose di questo prestigioso marchio.

Reply

iphone os 4
iphone os 4
7/23/2011 8:20:27 PM #

I wonder if he cheated on her? I remember he cheated on his previous wife with JLO so it wouldn’t be surprising.

Reply

philix
philix United States
11/19/2015 6:32:28 AM #

good web site

Reply

Normand Schwarz
Normand Schwarz United States
1/13/2016 1:26:31 PM #

I can't wait to read this masterpiece. Ordering off the menu at fast food restaurants and old dangerous playground equipment have to be in the book!

Reply

Jose Bacone
Jose Bacone United States
1/15/2016 4:58:52 AM #

this is awesome like the book of awesome

Reply

Loida Shaneyfelt
Loida Shaneyfelt United States
1/15/2016 5:00:10 AM #

Well good for you! I have a lot but one I just thought of was when you lick a crease of paper so that it'll rip in a straight line and it actually tears in a frayed straight line rather than rips awkwardly. I love that bleachy taste and that bleachy satisfaction.

Reply

Isreal Payor
Isreal Payor United States
1/15/2016 6:07:42 AM #

Printing out an essay or paper, reading the first line and realizing there aren't any mistakes.<br />AWESOME!

Reply

Natividad Sluder
Natividad Sluder United States
1/15/2016 6:43:17 AM #

getting the last pair of shoes in your size AWESOME!

Reply

Willis Karam
Willis Karam United States
1/15/2016 7:49:06 AM #

Congrats! This is such a great website, I know it will translate well into book form. Which is AWESOME!

Reply

Asanka Peidris
Asanka Peidris United States
7/20/2016 7:43:33 AM #

Take my word for it, it's not as easy as you think

Reply

Terrence Wenstrand
Terrence Wenstrand United States
8/25/2016 3:59:17 AM #

Hi I'm appreciating the time and energy you put into your post and detailed information you did. It’s awesome to come across a post every once in a while that isn’t the same out of date rehashed information. Fantastic read! I’ve bookmarked your site and I’m including your RSS feeds to my Yahoo account.

Reply

Rene Cimaglia
Rene Cimaglia United States
1/15/2017 3:36:56 AM #

Hit the streets with your mobile device or smartphone with your app loaded and get honest feedback from Joe Average. They may also get recommendations through online community site. Because of this, hourly rates for Burnaby mobile app, Richmond android development tend to reach as high as $150 an hour. The article speaks about the various ways to simplify our lives with the help of mobile apps.

Reply

downloading apk
downloading apk United States
4/15/2017 3:24:15 AM #

I found another website may download apk from Google Play: http://www.apksmart.com

Reply

Add comment

biuquote
  • Comment
  • Preview
Loading

Rel=Me

Month List

RecentComments

Comment RSS