Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Omer van Kloeten's Facebook profile

Omer has been professionally developing applications over the past 8 years, both at the IDF’s IT corps and later at the Sela Technology Center, but has had the programming bug ever since he can remember himself.
As a senior developer at NuConomy, a leading web analytics and advertising startup, he leads a wide range of technologies for its flagship products.

Get Firefox


powered by Dapper 

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

Announcing New C# 4.0 LINQ Features and Book

* Fabrice Marguerie, co-author of LINQ In Action, asked me to clarify that this post has absolutely nothing to do with their book and is simply meant in jest *

LINQ has been around for quite a while, making our lives easier with its short, declarative syntax. During talks in closed sessions, the persons behind C# have come up with some great plans for its next version, including some vast improvements to LINQ. The team wanted to make everyone's lives even better by introducing some ground-breaking features to it.

I'd like to introduce, out of those finalized or at the final draft stage, some of the new keywords that have been introduced to the language with regards to LINQ and by doing so, expose the reader to this new, exciting technology.

fine

Many of the lines of code incorporated into LINQ are about variable validation. How many times have you had to write pieces of code like this:

var values = from n in numbers
where n > 0 && n < 10
select n;

This piece of code is very trivial and the compiler is smart enough to understand it on its own. From C# 4.0, the fine keyword is introduced and the above query will be replaced with:

var values = from n in numbers
where n is fine
select n;

 

some

Most LINQ queries do not iterate over an entire enumeration. Some filter out results, some use Take, Skip and other methods in order to run on only a subset of the elements. Since this is an extremely common scenario, the team has decided to integrate it into the language using the new some keyword:

var values = from n in some numbers
select n;

This is complemented with the new most keyword, which is like some, only it takes more elements.

somewhere

Global Warming vs. LINQ from Clauses The from clause is known to all who use LINQ, as it indicates the source of the data. Since this clause is a recurring pattern (statistical surveys have shown it to occur 99% of the time, allowing for a 4% standard deviation), the team behind the new language features has decided to cut it out, thus removing unnecessary verbosity from the language. This, in turn, will cause less characters to be written and rewritten, meaning less keystrokes, less keyboard wear, less purchases of new keyboards, leading to prevention of global warming, a goal near to Microsoft's heart. You can see Microsoft's predictions for this in the graph to the right.

This is possible due to the new somewhere keyword, as described below:

var values = from somewhere
select n;

This feature relies heavily upon the newly introduced Type Inference Transfer System (abbreviated MANTA), a technology recently demonstrated by Microsoft Research.

The Book

LINQ InactionI've long since wanted to write a book about something. Since this is just the tip of the iceberg, I've decided to write my book about this subject. It will contain most of what I know (hey, I still need to speak about something at conferences and release a second edition some day, right?) about the new language. I think that with all of the new features, making our lives as developers that much easier, I've picked an appropriate title.

Here's an excerpt from the introduction:

Recent advances in compilation technologies have brought with them less need for verbosity. In its stead come smart compilers, which infer much of the work for the developer. With such technology, there's not a lot you, as a developer, need to do anymore. You can simply write a short statement, validate its correctness, check the code in and go home for the day.

Welcome to the world of tomorrow. Put your feet up.

Here's the layout of the book, as I plan it:

  1. Introduction
  2. New Features in LINQ

What else would you like to see in the book? Let me know and I might just send you a copy of the rough draft for review once I finish it.

Posted: Aug 29 2008, 01:12 PM by Omer van Kloeten | with 11 comment(s)
Filed under: ,

Comments

anon said:

Why is the book titled "LINQ INACTION" ? :)

# August 29, 2008 5:59 PM

Omer van Kloeten said:

Because you don't need to do anything anymore. It does everything for you. :)

# August 29, 2008 6:36 PM

Arjan`s World » LINKBLOG for August 30, 2008 said:

Pingback from  Arjan`s World    &raquo; LINKBLOG for August 30, 2008

# August 30, 2008 3:56 PM

Jason Haley said:

# August 31, 2008 12:44 PM

Sy said:

So what else you do to waste time (I mean other people's)?

# September 2, 2008 2:30 AM

Omer van Kloeten said:

I work at a shelter for the humorless. ;)

# September 2, 2008 2:44 AM

mad said:

was that some kind of joke?

# September 3, 2008 3:00 PM

Theraot said:

So we, the developers are losing control? Please tell me that "somewhere" will not read data from the recycle bin of my neighbor, I want to know from where the data came. I would like a solution like "with" in VB. I can see all those service packs solving errors with this, asking for 6GB out your hard disk every time.... ºo.Oº

# October 2, 2008 4:24 AM

Ye gods said:

Have you all had sense of humour bypasses? Bloody hell, I bet you're Americans. ;)

I must say I'm a bit disappointed with the standard of the changes. I am disappointed to find that my own suggestions to Microsoft recently have been discarded.

Just for reference they were along the following lines;

I had a similar idea to enhance LINQ along the lines of the changes to "from", except I suggested the use of the much snappier "get". Simply using "get" would already yield a 25% increase in productivity, but wait until you get a load of the syntax.

I find the replacement suggestion of "from somewhere" as a way of determining a resource to choose values unnecessarily verbose. The keyword "somewhere" can easily be inferred through one's surroundings.

Similarly the whole "select blah" statement is completely redundant, simply from the use of LINQ - why use it unless you want to get something?

So my revised LINQ 4++ syntax could distill a statement such as this:

var values = from somewhere

            select n;

down to this:

var values = get stuff;

# October 9, 2008 10:19 AM

Erich Bühler said:

I would also like to see something like this:

Class myObject = new ;

instead of:

Class myObject = new Class();

Where Class is obviously not an interface (and default constructor is used). I could also accept something like this:

new Class myObject = ;

He he he, VB is back ;-)

Regards,

Erich.

# October 28, 2008 5:56 AM

Bill said:

Had nothing to do today, so I thought I'd send this down to the c# team.

What we need is merge and hash joins for in memory queries.

       private string test(int c)

       {

           List<int> list1 = new List<int>() { 3, 1, 2, 6, 7, 8, 30 };

           List<int> list2 = new List<int>() { 4, 5, 2, 2, 7, 9, 30 };

           (from l1 in list1 from l2 in list2 select (l1 * l2 + 111) % 77).ToList<int>().ForEach(n => { if (list1.Count < c) list1.Add(n); });

           (from l1 in list1 from l2 in list2 select (l1 * l2 + 111) % 77).ToList<int>().ForEach(n => { if (list2.Count < c) list2.Add(n); });

           (from l1 in list1 from l2 in list2 select (l1 * l2 + 111) % 77).ToList<int>().ForEach(n => { if (list1.Count < c) list1.Add(n); });

           (from l1 in list1 from l2 in list2 select (l1 * l2 + 111) % 77).ToList<int>().ForEach(n => { if (list2.Count < c) list2.Add(n); });

           Stopwatch s = new Stopwatch();

           s.Start();

           int lLoop = InnerLoopJoin(list1, list2);

           s.Stop();

           long tLoop = s.ElapsedMilliseconds;

           s.Reset();

           s.Start();

           int lHash = InnerHashJoin(list1, list2);

           s.Stop();

           long tHash = s.ElapsedMilliseconds;

           s.Reset();

           return "Hash = " + tHash.ToString() + "  Loop = " + tLoop.ToString();

       }

       private void button1_Click(object sender, EventArgs e)

       {

           StringBuilder sb = new StringBuilder();

           for (int i = 100; i < 4000; i += 100)

               listBox1.Items.Add(i.ToString() + test(i));

       }

       private int InnerLoopJoin(List<int> list1, List<int> list2)

       {

           var x =

               from l1 in list1

               from l2 in list2

               where l1 == l2

               select new { l1, l2 };

           return x.Count();

       }

       private int InnerHashJoin(List<int> list1, List<int> list2)

       {

           Dictionary<int, int> d = (from l1 in list1 group l1 by l1 into g select g).ToDictionary(n => n.Key, n => n.Count());

           var x =

               from l2 in list2

               where d.ContainsKey (l2)

               from i in Enumerable.Range (1, d[l2])

               select new { l1 = l2, l2 };

           return x.Count();

       }

       private int MergeJoin(List<int> list1, List<int> list2)

       {

           // Had nothing to do today until now.

           return 0;

       }

# October 30, 2008 12:09 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)