Wednesday, February 10, 2010

Language Integrated Query (Linq) Session @ HM

Ok, seems like i always get the talks nobody else want to have. Mh, what can i say about Linq? Well i tend to tell the people how things work. But explaining how they build Linq is a hell of hack. Yes i can tell them about “computable functions” or let’s define better “higher-order functions”. WTF? So where to start? Functional Programming? Or maybe the Lambda calculus? Well i don’t think so. And reading the the Paper? “Can Programming Be Liberated from the von Neumann Style? A Functional Style and Its Algebra of Programs” written by John Backus at the IBM Research Laboratory, San Jose. Is this the start for my Session?

I often come to the conclusion people don’t want to know anything. Like in the movie “The Island” where McCord says: “Just cause people wanna eat the burger doesn't mean they wanna meet the cow.” And talking to students @ University of Applied Sciences Munich is always a pleasure. Give them a ball, and let them run with it. You’ll be amazed by the things they will come back with.

So, decided - i show the Technology and explain it step by step. Perhaps they will surprise me again.

So showing how to work with Linq, you can find so many solutions …

  • Perhaps you start with reading?

Linq start Webpage @ Microsoft
Thank you Charlie, again!

  • Or maybe you want to check out some Webcasts?

MSDN Webcast: Introduction to Microsoft .NET Language Integrated Query
and even more Linq Webcasts …

  • i for myself prefer the code:

So one step could be Open Visual Studio –> Help –> Samples –> local Sample folder –> CSahrpSamples.zip –> Linq Samples
or direct via C:\Program Files\Microsoft Visual Studio 10.0\Samples\1033

another way of learning by code start here … CodeProject … or look closer to Charlie’s Blog.

And yes, there are Books to:

Introducing Microsoft LINQ
byPaolo Pialorsi and Marco Russo
Microsoft Press 2007 (240 pages)
ISBN:9780735623910

Save the money, give it to me. :-) The book is not as good as his title promises. Since it’s the only one i read about this topic, i can’t tell much about others.

So i finally mixed up this things with an early preview of Anders Hejlsberg and came to this solution.

The Presentation stuff:

Luca Bolognese introduces Linq always by saying developers != plumper's. What that means is, like any other O/R Mapper Linq to SQL is doing the Mapping of SQL data - Types to you’re development data – Types. Well i prefer to introduce Linq by first of all telling the folks about Duck typing.

duck     

What you can do with Duck typing is you can use a shorter way to define your variables. Instead of using something like

   1:  int i = 10;

you can do the same thing by typing

   1:  var i = 10;

Ok, that however doesn’t means that it is a non strictly typed variable. Since the compiler understands that 10 is a integer value. Where is the duck? Well the duck means – it smells like a duck, it looks like a duck, it even acts like a duck ==> so it must be a duck.

Sine 10 is obviously a integer value C# knows that and using the best fitting data type which is int. Well you may argue: “Where is the benefit?”. There are 2 profits of Duck typing. The first one is easy to explain. Think of declaring a variable of a class or .net Type like 

System.Security.Cryptography.ManifestSignatureInformationCollection collection = new ManifestSignatureInformationCollection();

ok, don’t you think that this is a very long way telling the compiler what you mean. A shorter way would be:

var collection = new ManifestSignatureInformationCollection();

I personally like that profit. Using the var when you declare a string, int, … i personally don’t like that way. It makes the code a little bit harder to understand. Because i don’t see it on the first look, what the code is trying to tell me. I have to think a second about it. Ok it’s just a second but it’s still a second.

The other benefit is by using Linq. You really don’t have to think about the result you get back from your query. Telling about queries it’s time to talk about Linq in general.

With Linq you be able to query against anything which implements IEnumerable. Since any Object and data type in dotnet is using this Interface you be able to query against it.

Another aspect on Linq is that you can query against wrappers for databases, XML files and a lot of other implantations.

Now comes the part to explain Lambda expressions and the query style. 1st against Objects, 2nd against the O/R Mapper and 3rd against XML.

Since this blog post already a bit long and i don’t want to bore you any further here are coming the links. Tada…

So here you can find the empty Demo Project.

And here you can find the solved Demo Project.

Last but at always not least, the presentation.

So one good suggestion i can give you, think twice before you use “Linq to SQL”. Don’t think twice if you use Datasets. I have the hunch that Datasets are the hypocrite of death (memory). RAM Footprints, Race conditions, bad performance … are just a view results of using Datasets. “Linq to SQL” instead is a O/R Mapper. It helps a lot and brings performance to your design time. But performance and persistence problems at runtime could occur. If you have a performance critic solution, don’t touch “Linq to SQL”. And using the Entity Framework which means ADO.net makes it worse. Do it by hand - is the only way. Use stored procedures and let the databases do the things they are made for. Yes you can use stored procedures and that stuff in Linq too. But how do you get rid of the caching, the overcharged objects and the heavy memory usage of “Linq to SQL”?         

No comments: