Reinventing LINQ

by Matt 20. March 2008 19:54

Daniel Cazzulino made an interesting post a little while ago that I've been meaning to follow up. Ian Griffiths picked up on one aspect of it (the interesting, obscure type inference issue - and I love the idea of "mumble types") which reminded me to go have another look.

The gist of Daniel's post is to rewrite some Ruby code using some of the nice new C# features. The Ruby code is using Fibers (think iterators) to take a sequence as input, and output a new sequence of different values, or a new sequence made by skipping values.

And I really had to torture that sentence to avoid saying "map" and "filter". Remember, they're the magic words...

Daniel follows the original article very faithfully. He replaces the Fibers with iterators, adds some syntax sugar with extension methods and lambda functions, and duplicates the functionality of LINQ. He just stops short of using the nice syntax. So let's have a look at the last few examples.

Get 10 integers that are both even and multiples of three:

var evenMultiplesOfThree = from x in Enumerable.Range(1, 1000)
                           where (x % 2) == 0 && (x % 3) == 0
                           select x;
foreach (int i in evenMultiplesOfThree.Take(10))

(OK, slightly different. Daniel's range is infinite, I've capped mine.)

The pipeline for tripling even numbers, adding one and only printing the first 5 multiples of 5 (and this one lets me use "let"):

var results = from x in Enumerable.Range(1, 1000)
              let y = (x * 3) + 1
              where (y % 5) == 0
              select y;
foreach (int i in results.Take(5))

And the (somewhat unwieldy) palindrome finder:

var words = "Madam, the civic radar rotator is not level".Split(' ');
var palindromes = from word in words
                  let normalised = new { 
                      Original = word, 
                      Normalised = word.ToLower() 
                  select "'" + normalised.Original + "' is " + 
                    ((normalised.Normalised != new string(normalised.Normalised.Reverse().ToArray()))
                      ? "not " : "") + "a palindrome";
foreach(var palindrome in palindromes)

But this is an awkward query. Perhaps a poor example to convert into a query, and a simple loop might have served better.

Personally, I don't like the idea of having a ForEach extension method. I like doing the explicit foreach command. I think it separates the enumeration over the results from the description of the query (pipeline).

I think Daniel's post validates the design of LINQ. Independently, he took iterators, lambdas and extension methods and built something that, apart from the syntax, is LINQ. That's good design, and shows that LINQ is really evolutionary in the way it's built on so many smaller features in the runtime. But by not going that final step, it also shows that LINQ, and especially the query syntax, is really revolutionary. I think it's quite a mind shift to go from a "pipeline", a processing concept that's very procedural, explicitly procedural, and convert it into a query, something that's much more declarative.


Comments (40) -

best suv
best suv
7/20/2011 10:30:13 PM #

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


best hybrid cars
best hybrid cars
7/22/2011 4:14:19 AM #

Is it ok if I quote your article in my monthly newsletter? I would think this article suits my topic perfectly. Well ya, thanks for posting this article.


iphone developer program
iphone developer program
7/24/2011 5:51:47 AM #

Is it ok if I quote your article in my monthly newsletter? I would think this article suits my topic perfectly. Well ya, thanks for posting this article.


Philix United States
11/21/2015 3:00:08 AM #

Good Website.


Natividad Sluder
Natividad Sluder United States
1/7/2016 2:53:04 AM #

Let me be the first to say: Congrats, Neil! I've been following this site from day one, and have loved every single day of awesomeness that it has delivered. You deserve all the success coming your way. Keep it up!AWESOME!


Raleigh Maarx
Raleigh Maarx United States
1/15/2016 8:32:36 AM #

getting the last pair of shoes in your size AWESOME!


Rodolfo Thomases
Rodolfo Thomases United States
1/15/2016 8:55:35 AM #

Congratulations on your success! You deserve it for brightening all of my work days with something to be excited about.


Chad Cassem
Chad Cassem United States
7/25/2016 3:21:50 PM #

Times are changing and so as technology. We hope to find smart home architectural planners using the updated technologies obtainable in the market.


Scott Kevin
Scott Kevin United States
8/5/2016 5:56:54 AM #

Представляем новый веб- сайт знакомств будет оштрафован на широкий свою настоящую любовь в


Frances Ward
Frances Ward United States
8/16/2016 8:42:59 PM #

F*ckin’ awesome issues here. I’m very satisfied to peer your article. Thanks so much and i am looking forward to touch you. Will you kindly drop me a mail?


Mathilda Kieser
Mathilda Kieser United States
8/25/2016 4:27:58 AM #

Hi First of all I would like to say what a nice blog! I had a quick question that I’d like to ask if you don’t mind. I was curious to understand how you center yourself and clear your mind prior to blogging. I’ve had trouble clearing my thoughts in getting my thoughts out. I do enjoy posting blog but it just seems like the first 10 to 15 minutes are unproductive simply just trying to figure out how to begin. Any thought or tips? Thank you!


James Fly
James Fly United States
9/25/2016 10:09:31 PM #

Это такой хороший сайт с большим количеством полезной информации . Приходите присоединиться к нашему новый бесплатный социальный сайт, как Facebook . У нас есть группы друзей , объявления, сайт знакомств играть в игры бесплатно для всех на


baexkitty United States
5/22/2018 10:17:57 PM #

She may take her panties off at any time!


Mason Flanery
Mason Flanery United States
9/27/2018 8:43:09 PM #

Do you mind if I quote a few of your posts as long as I provide credit and sources back to your blog? My website is in the very same area of interest as yours and my users would really benefit from some of the information you present here. Please let me know if this ok with you. Cheers!


Marian Overholtzer
Marian Overholtzer United States
9/28/2018 2:03:12 AM #

Great post. I was checking constantly this blog and I am impressed! Very useful information particularly the remaining section Smile I take care of such info a lot. I used to be seeking this particular information for a very lengthy time. Thank you and best of luck.


Arielle Kha
Arielle Kha United States
9/28/2018 8:23:07 AM #

You can definitely see your skills in the work you write. The world hopes for even more passionate writers like you who are not afraid to say how they believe. Always go after your heart.


Ulysses Laduke
Ulysses Laduke United States
9/28/2018 11:07:33 PM #

Wow that was unusual. I just wrote an very long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyway, just wanted to say fantastic blog!


Judson Chanco
Judson Chanco United States
9/29/2018 5:03:34 AM #

I’ve just been talking to Sean Gallagher about his upcoming Instant Income Cash Machine course, and he’s been kind enough to fill me in on a few details regarding his upcoming course.


Gwyneth Applonie
Gwyneth Applonie United States
9/29/2018 11:12:53 AM #

You made some respectable factors there. I appeared on the internet for the issue and found most individuals will associate with together with your website.


Lucius Soza
Lucius Soza United States
9/29/2018 3:08:34 PM #

Nice read, I just passed this onto a friend who was doing a little research on that. And he actually bought me lunch as I found it for him smile So let me rephrase that: Thanks for lunch!


Latanya Merschman
Latanya Merschman United States
9/29/2018 11:56:58 PM #

It’s great page, I was looking for something like this


Pennie Hedtke
Pennie Hedtke United States
10/8/2018 9:57:27 PM #

I don?t even know how I ended up here, but I thought this post was good. I do not know who you are but definitely you're going to a famous blogger if you are not already ;) Cheers!


Leonor Volland
Leonor Volland United States
10/9/2018 4:39:33 AM #

Just about all of the things you articulate is astonishingly appropriate and it makes me wonder why I hadn't looked at this with this light before. Your piece truly did switch the light on for me personally as far as this particular subject goes. Nevertheless at this time there is actually 1 issue I am not necessarily too comfy with and while I make an effort to reconcile that with the actual central theme of your issue, let me see just what the rest of your visitors have to point out.Very well done.


Responsive Website Design
Responsive Website Design United States
10/9/2018 1:23:12 PM #



Stacy Pleasanton
Stacy Pleasanton United States
10/9/2018 9:16:32 PM #

Oh my goodness! a tremendous article dude. Thank you However I am experiencing problem with ur rss . Don’t know why Unable to subscribe to it. Is there anyone getting similar rss downside? Anyone who is aware of kindly respond. Thnkx


Mike Brezeale
Mike Brezeale United States
10/9/2018 10:20:36 PM #

hello!,I like your writing so much! share we communicate more about your post on AOL? I need a specialist on this area to solve my problem. May be that's you! Looking forward to see you.


Rocco Shultis
Rocco Shultis United States
10/10/2018 7:10:52 AM #

It's perfect time to make some plans for the future and it is time to be happy. I have read this post and if I could I wish to suggest you some interesting things or advice. Perhaps you can write next articles referring to this article. I desire to read more things about it!


玻尿酸隆鼻失敗 United States
10/10/2018 10:26:45 AM #

豐盈水潤,造就柔嫩飽滿,滋潤有彈性、晶瑩煥白的肌膚。 乾性肌膚使用


醫學美容 爽身
醫學美容 爽身 United States
10/10/2018 9:10:28 PM #

到底Meghan Markle是甚麼來頭?她憑什麼能俘獲英國二王子?    Marie Claire (HK) Edition  又有一名鑽石王老五被封盤了!這位還要是一位王子啊!相信大家都知道英國哈里王子已經宣布要迎娶美國女演員Meghan Markle了 ,二人很快地就接受了訪問與大家分享好消息。 哈里王子與Meghan是    


Percy Hilke
Percy Hilke United States
10/11/2018 7:39:47 AM #

You made some respectable factors there. I regarded on the web for the difficulty and located most people will go along with together with your website.


Elwood Buemi
Elwood Buemi United States
10/11/2018 12:11:26 PM #

I like the helpful info you provide in your articles. I will bookmark your weblog and check again here regularly. I'm quite certain I?ll learn many new stuff right here! Good luck for the next!


Dorla Feinman
Dorla Feinman United States
10/11/2018 4:39:52 PM #

F*ckin’ remarkable things here. I am very happy to peer your article. Thanks so much and i am looking forward to touch you. Will you please drop me a mail?


Laraine Zboral
Laraine Zboral United States
10/12/2018 6:00:27 AM #

Wow! This blog looks exactly like my old one! It's on a totally different topic but it has pretty much the same layout and design. Superb choice of colors!


Corrine Bernosky
Corrine Bernosky United States
10/14/2018 2:17:30 AM #

Youre so cool! I dont suppose Ive read anything similar to this before. So nice to discover somebody with original ideas on this subject. realy thank you for starting this up. this excellent website is one area that is needed on-line, an individual if we do originality. valuable project for bringing new things towards the internet!


醫學美容 Derma Veil 童顏針
醫學美容 Derma Veil 童顏針 United States
10/14/2018 5:05:46 PM #

Derma Veil ® 被喻為新世代逆齡完美輪廓塑造,最新一代的PLLA膠原。 2003年獲得Mexican Ministry of Health (SSA)認證及美國FDA出口認證,並於2006年在拉丁美洲及遠東至東南亞地區廣泛使用,多個臨床實例見證能改善老化、遺傳、疾病(如脂肪萎縮)等引起的凹陷問題,其效果備受認同。蘊含兩大活性成分均具有生物兼容性及分解性,可逐步被人體自然分解吸收,有效塑造童顏肌 : 1. 聚左乳酸 (Poly-L-lactic acid / PLLA) : 促進骨膠原生長 2. 甘醇酸(Glycolic Acid ) 使皮膚表皮層黏膠性脂質鬆軟,改善皮膚厚度,加速細胞再生,減少皺紋及疤痕,加強保濕功能,增加光澤,美白效果。 由於甘醇酸分子較小,容易滲透皮膚 治療前: 皮膚的凹陷/皺紋 治療後: 成分被人體吸收、並刺激膠原增生,撫平皺紋及凹陷部位。 注入BOTOX(保妥適)會抑制突觸前膜釋放神經遞質,阻斷乙酰膽鹼(Acetylcholine)的釋放,從而使肌肉張力下降或癱瘓麻痺,皺紋也隨之而逐漸消失。


Maryjane Homme
Maryjane Homme United States
10/14/2018 10:48:40 PM #

Thanks  for every other informative web site. Where else may I get that type of info written in such an ideal method? I have a venture that I'm just now operating on, and I've been on the look out for such information.


醫學美容 養護
醫學美容 養護 United States
10/17/2018 4:18:40 PM #

LoveVivi & Vstyle 【馬甲彩妝系列】迷你偽裝寶盒的商品介紹   LoveVivi & Vstyle,馬甲彩妝系列,迷你偽裝寶盒       


Erminia Aronica
Erminia Aronica United States
10/19/2018 3:06:05 AM #

Excellent goods from you, man. I have understand your stuff previous to and you are just too magnificent. I actually like what you have acquired here, really like what you're stating and the way in which you say it. You make it entertaining and you still care for to keep it sensible. I can not wait to read far more from you. This is actually a terrific site.


Shaunna Vuillemot
Shaunna Vuillemot United States
10/20/2018 12:06:06 AM #

We're a group of volunteers and starting a new scheme in our community. Your site offered us with valuable info to work on. You have done an impressive job and our whole community will be thankful to you.


Pat Veasman
Pat Veasman United States
10/20/2018 1:13:44 PM #

I discovered your weblog web site on google and test a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN Information Reader. Searching for ahead to studying more from you later on!…


Add comment

  • Comment
  • Preview


Month List


Comment RSS