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.

Advertisements

9 thoughts on “Announcing New C# 4.0 LINQ Features and Book

  1. 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º

  2. 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;

  3. 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.

  4. 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 list1 = new List() { 3, 1, 2, 6, 7, 8, 30 };
    List list2 = new List() { 4, 5, 2, 2, 7, 9, 30 };
    (from l1 in list1 from l2 in list2 select (l1 * l2 + 111) % 77).ToList().ForEach(n => { if (list1.Count < c) list1.Add(n); });
    (from l1 in list1 from l2 in list2 select (l1 * l2 + 111) % 77).ToList().ForEach(n => { if (list2.Count < c) list2.Add(n); });
    (from l1 in list1 from l2 in list2 select (l1 * l2 + 111) % 77).ToList().ForEach(n => { if (list1.Count < c) list1.Add(n); });
    (from l1 in list1 from l2 in list2 select (l1 * l2 + 111) % 77).ToList().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 list1, List 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 list1, List list2)
    {
    Dictionary 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 list1, List list2)
    {
    // Had nothing to do today until now.
    return 0;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s