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 (81) -

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

Dick Jews
Dick Jews United States
5/27/2017 10:18:39 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Hipolito Pavel
Hipolito Pavel United States
5/27/2017 10:18:50 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Phil Emch
Phil Emch United States
5/27/2017 10:19:19 AM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Salina Jimison
Salina Jimison United States
5/27/2017 10:20:34 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Janean Gusman
Janean Gusman United States
5/27/2017 11:29:46 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Norman Dewey
Norman Dewey United States
5/27/2017 6:01:17 PM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Kenton Katin
Kenton Katin United States
5/27/2017 9:52:59 PM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Benedict Garth
Benedict Garth United States
5/27/2017 9:55:07 PM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Twyla Clymer
Twyla Clymer United States
5/27/2017 10:00:30 PM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Ewa Batalona
Ewa Batalona United States
5/27/2017 10:01:01 PM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Alex Dziadek
Alex Dziadek United States
5/28/2017 1:43:05 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Bradley Elefritz
Bradley Elefritz United States
5/28/2017 3:11:34 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Antoine Epling
Antoine Epling United States
5/28/2017 3:12:15 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Quentin Medlen
Quentin Medlen United States
5/28/2017 3:16:51 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Bettye Barbar
Bettye Barbar United States
5/28/2017 3:17:03 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Miguel Mcgarity
Miguel Mcgarity United States
5/28/2017 5:36:26 AM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

John Eichelmann
John Eichelmann United States
5/28/2017 7:05:16 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Garfield Tippens
Garfield Tippens United States
5/28/2017 9:05:45 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Willa Moyers
Willa Moyers United States
5/28/2017 9:06:21 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Benedict Garth
Benedict Garth United States
5/28/2017 9:41:19 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Russell Pead
Russell Pead United States
5/28/2017 10:12:51 AM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Valentina Probert
Valentina Probert United States
5/28/2017 10:44:19 AM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Edythe Baisley
Edythe Baisley United States
5/28/2017 10:44:21 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Ulysses Keogh
Ulysses Keogh United States
5/28/2017 10:45:31 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Mora Caughey
Mora Caughey United States
5/28/2017 10:45:37 AM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Bettina Megginson
Bettina Megginson United States
5/28/2017 1:26:18 PM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Eddy Mardirossian
Eddy Mardirossian United States
5/28/2017 2:45:50 PM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Leopoldo Himmons
Leopoldo Himmons United States
5/28/2017 5:54:45 PM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Leslie Darsey
Leslie Darsey United States
5/28/2017 8:16:56 PM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Clayton Romane
Clayton Romane United States
5/28/2017 8:20:29 PM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Werner Alzaga
Werner Alzaga United States
5/28/2017 8:25:25 PM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Major Consla
Major Consla United States
5/28/2017 8:52:27 PM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Kathey Desatnik
Kathey Desatnik United States
5/28/2017 9:29:57 PM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Homer Skibosh
Homer Skibosh United States
5/28/2017 9:32:45 PM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Joye Jeffery
Joye Jeffery United States
5/28/2017 9:43:04 PM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Pasty Bardoner
Pasty Bardoner United States
5/28/2017 10:16:02 PM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Kerry Laipple
Kerry Laipple United States
5/28/2017 10:37:00 PM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Pasty Bardoner
Pasty Bardoner United States
5/28/2017 11:34:40 PM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Domingo Frigge
Domingo Frigge United States
5/29/2017 12:00:47 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Pasquale Camero
Pasquale Camero United States
5/29/2017 12:14:32 AM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Sherman Pock
Sherman Pock United States
5/29/2017 12:15:28 AM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Byron Sentinella
Byron Sentinella United States
5/29/2017 12:27:07 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Elsy Dooms
Elsy Dooms United States
5/29/2017 1:01:38 AM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Milan Pozzuoli
Milan Pozzuoli United States
5/29/2017 3:02:41 AM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Tommie Trautwein
Tommie Trautwein United States
5/29/2017 4:11:26 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Lucienne Loura
Lucienne Loura United States
5/29/2017 4:36:42 AM #

[url=https://cashloansyes.org/]Cash Advance Loans[/url] <a href="https://cashloansyes.org/">Cash Advance Loans</a>  https://cashloansyes.org

Reply

Jaime Bogatitus
Jaime Bogatitus United States
5/29/2017 5:47:38 AM #

[url=https://cashloansyes.org/]Cash Loans[/url] <a href="https://cashloansyes.org/">Cash Loans</a>  https://cashloansyes.org

Reply

Tamar Kaeding
Tamar Kaeding United States
5/29/2017 10:23:21 AM #

[url=http://krasotamoda.ru/]Рамадан расписание[/url] http://krasotamoda.ru/ <a href="http://krasotamoda.ru/">;Рамадан 2017</a>

Reply

Virgil Smialek
Virgil Smialek United States
5/29/2017 10:47:26 AM #

[url=http://mastuclub.ru/]Ramadan 2017 [/url] http://mastuclub.ru/ <a href="http://mastuclub.ru/">Ramadan</a>;

Reply

Nathanial Cangiano
Nathanial Cangiano United States
5/29/2017 10:50:42 AM #

[url=http://krasotamoda.ru/]Ramadan 2017 [/url] http://krasotamoda.ru/ <a href="http://krasotamoda.ru/">Ramadan 2017 </a>

Reply

Rickey Lehne
Rickey Lehne United States
5/29/2017 11:52:23 AM #

[url=http://mercedes50.ru/]Ramadan raspisanie[/url] http://mercedes50.ru/ <a href="http://mercedes50.ru/">Ramadan 2017 </a>

Reply

Stephan Hanhardt
Stephan Hanhardt United States
5/29/2017 12:06:01 PM #

[url=http://mastuclub.ru/]Ramadan raspisanie[/url] http://mastuclub.ru/ <a href="http://mastuclub.ru/">Ramadan 2017 </a>

Reply

Cornell Bumby
Cornell Bumby United States
5/29/2017 12:23:07 PM #

[url=http://youmillioner.ru/]Ramadan raspisanie[/url] http://youmillioner.ru/ <a href="http://youmillioner.ru/">Ramadan</a>;

Reply

Maxie Blasini
Maxie Blasini United States
5/29/2017 12:23:09 PM #

[url=http://shakya.ru/]Ramadan[/url] http://shakya.ru/ <a href="http://shakya.ru/">Ramadan raspisanie</a>

Reply

Justin Romanski
Justin Romanski United States
5/29/2017 12:23:27 PM #

[url=http://mastuclub.ru/]Ramadan raspisanie[/url] http://mastuclub.ru/ <a href="http://mastuclub.ru/">Ramadan raspisanie</a>

Reply

Princess Cowdin
Princess Cowdin United States
5/29/2017 12:42:20 PM #

[url=http://shakya.ru/]Ramadan 2017 [/url] http://shakya.ru/ <a href="http://shakya.ru/">Ramadan</a>;

Reply

Julietta Fagin
Julietta Fagin United States
5/29/2017 12:48:47 PM #

[url=http://mercedes50.ru/]Ramadan raspisanie[/url] http://mercedes50.ru/ <a href="http://mercedes50.ru/">Ramadan raspisanie</a>

Reply

Carmelita Dapvaala
Carmelita Dapvaala United States
5/29/2017 12:48:51 PM #

[url=http://krasotamoda.ru/]Ramadan raspisanie[/url] http://krasotamoda.ru/ <a href="http://krasotamoda.ru/">Ramadan</a>;

Reply

Clair Absher
Clair Absher United States
5/29/2017 1:02:08 PM #

[url=http://mercedes50.ru/]Ramadan 2017 [/url] http://mercedes50.ru/ <a href="http://mercedes50.ru/">Ramadan 2017 </a>

Reply

Jessie Satiago
Jessie Satiago United States
5/29/2017 1:04:39 PM #

[url=http://vkusno-recept.ru/]Ramadan 2017 [/url] http://vkusno-recept.ru/ <a href="http://vkusno-recept.ru/">Ramadan 2017 </a>

Reply

Lorita Rodell
Lorita Rodell United States
5/29/2017 1:16:39 PM #

[url=http://krasotamoda.ru/]Ramadan 2017 [/url] http://krasotamoda.ru/ <a href="http://krasotamoda.ru/">Ramadan 2017 </a>

Reply

Cletus Tamporello
Cletus Tamporello United States
5/29/2017 1:31:12 PM #

[url=http://krasotamoda.ru/]Ramadan 2017 [/url] http://krasotamoda.ru/ <a href="http://krasotamoda.ru/">Ramadan raspisanie</a>

Reply

Craig Grounds
Craig Grounds United States
5/29/2017 1:31:33 PM #

[url=http://scalatradeoptions.ru/]Ramadan raspisanie[/url] http://scalatradeoptions.ru/ <a href="http://scalatradeoptions.ru/">Ramadan raspisanie</a>

Reply

Sherlene Wojeik
Sherlene Wojeik United States
5/29/2017 1:35:47 PM #

[url=http://norma-next.ru/]Ramadan raspisanie[/url] http://norma-next.ru/ <a href="http://norma-next.ru/">Ramadan</a>;

Reply

Iraida Samas
Iraida Samas United States
5/29/2017 1:43:28 PM #

[url=http://krasotamoda.ru/]Ramadan[/url] http://krasotamoda.ru/ <a href="http://krasotamoda.ru/">Ramadan 2017 </a>

Reply

Add comment

biuquote
  • Comment
  • Preview
Loading

Rel=Me

Month List

RecentComments

Comment RSS