TheChaseMan's Frenetic SoapBox

Always looking for better ways to do things...

PDC 2005, C# 3.0, LINQ, and all kinds of cool stuff!

My oh my are the blogs pumping with action today! Everything from the Bill Gates keynote at PDC to all of this really interesting technology being developed for the Windows and .NET platforms. Let me just state up front that if you are a C# developer haven't seen this video on Channel 9 about Language Integrated Query (LINQ), go watch it now!

So hopefully you've already seen the video at this point .Oh and guess what? You can download LINQ right now too. Sorry, didn't mean to leave out the VB developers - this is a .NET thing, not just a C# thing. VB9 and C# 3.0 are going to rock! I'm not going to be able to do this video justice, so let me just ask you if you haven't seen the video at this point:  How cool would it be to be able to query XML, WinFS, or even objects in a collection doing something like this:

//List being used for example based on NorthWind

var results =
    from c in customers
    where c.City == "London"
    select c

So now I know you are going to watch the video - haha. Yes, you can do joins, grouping, order by, etc, etc too! Dan Fernandez has a lot better examples than mine here.  What you saw, or should have seen in the video, was a lot of new, interesting things in C# that you can and can read about in the C# 3.0 language spec.  Anders explains the use of implicitly typed local variables in C# 3.0 where you use the "var" keyword to do things like:

var customers = new List();

Furthermore, he uses anonymous types. These are a lot like using delegates to create an anonymous methods where the object type is inferred, but you get the all-important type-checking by the compiler. So you can do something like this:

var person = new {Name = "Bob", Age = 33};
Console.WriteLine(person.Name, person.Age);

One of the things I don't think I saw in the video that you can read about in the C# 3.0 language spec is: lambda expressions. If you've seen some of the freaky examples of anonymous methods, you'll like these even more because you can do things like: calc the sum of numbers this way:

(int x) => (x * (x + 1) / 2)

   - or -

//determine senior discount
Func<int, bool>  f = n => n >= 55;
bool giveDiscount = f(person.Age);

Note that in the above example, Func is a class in the System.Query namespace and it is "generic delegate" type that you can create dynamically so you don't have to formally declare a delegate. Now imagine using lambda expressions with LINQ to do stuff like this:

Func<string, bool> filter = s => s.Length == 5;
Func<string, string> extract = s => s;
Func<string, string> project = s => s.ToUpper();

IEnumerable<string> expr = names.Where(filter)
                                .OrderBy(extract)
                                .Select(project);

 

There's a whole lot more to the query operators you can use, and even something called Xlinq for XML-specific functionality! Along these same lines is something called the OfType keyword that I found pretty interesting. imagine you have an array of objects containing different types of values such as ints, doubles, strings, etc. If you want to extract only the integer values, this is where OfType comes in handy:

object[] bunchOfStuff = {"Foo", 1, 2.0, 3, "Bar"};
IEnumerable theIntegersInStuff = bunchOfStuff.OfType();
 

So, there are a lot of really interesting things to look forward to in the future. I for one and pretty psyched up about it. .NET v2.0 isn't even RTM yet and already I want v3.0. :-)


Digg!

posted on Tuesday, September 13, 2005 6:22 PM

Feedback

# DLinq 9/13/2005 8:24 PM TheChaseMan's Frenetic SoapBox

# DLinq 9/13/2005 8:37 PM TheChaseMan's Frenetic SoapBox

# What is the world saying about Microsoft's C# and VB LINQ Project 9/14/2005 10:51 AM Barry Gervin's Software Architecture Perspectives

# What is the world saying about Microsoft's C# and VB LINQ Project 9/14/2005 11:45 AM Barry Gervin's Software Architecture Perspectives