This document discusses the evolution of LINQ (Language Integrated Query) in .NET from version 1.0 through 3.5, including the introduction of lambda expressions and extension methods. Key concepts covered include LINQ query syntax, Enumerable extension methods like Where, Select, OrderBy, and Joining queries. Examples demonstrate basic LINQ queries on collections and joining data from multiple sources.
38. var slowQuery =
from c in customers
from p in purchases where c.ID == p.CustomerID
select c.Name + " bought a " + p.Description;
var fastQuery =
from c in customers
join p in purchases on c.ID equals p.CustomerID
select c.Name + " bought a " + p.Description;
40. var easyToRead =
from c in customers
join p in purchases on c.ID equals p.CustomerID
select c.Name + " bought a " + p.Description;
var harderToRead = customers.Join (
purchases,
c => (int?)(c.ID),
p => p.CustomerID,
(c, p) => ((c.Name + " bought a ") +
p.Description)
);
#4: C# 2 introduced generics, at that point, C# is feature complete from an OO perspective
#5: Next challenge to tackle after OO features is Big Data, how to reduce complexity of integrating information
#6: Have to juggle translation between database constructs and objects
#7: Easy way to query data source
Extension methods, query comprehension syntax
Way to return arbitrary results
Anonymous types, var keyword
Way to pass input data
Closures, collection and object initializers
#11: May be too complicated to know at design time, but known at compile time. For more flexibility, you need dynamics and the DLR in .NET 4.
#12: For the last piece of the puzzle, lets talk about variable binding
#18: System.Void isnt a true Type
Image: http://www.efunda.com/math/bessel/images/BesselJPlot.gif
#19: Need a separate generic delegate for void return methods
Image: http://portcityfilmfestival.com/clap-board.jpg
#20: Distill down to simpler forms, all equivalent
Image: http://watermarked.cutcaster.com/cutcaster-photo-100783484-Lambda-symbol-in-glass-3d.jpg
#21: The term lambdas comes from lambda calculus. Instead of set-based theories like Venn diagrams, Church wanted to look at it from a functional design perspective.
#25: In an operation like adding, I can substitute the functions for the numbers
#26: If I can do that, adding itself can be defined as a function.
Adding has no side effects. 5 is still 5 after I use it while adding.
#27: If you take this to its logical conclusion, any group of operations can be expressed as a hierarchy of functions which C# calls Expression Trees.
#28: But if thats true, it makes sense that I can write some function-by-function transformations that turn a C# function into a SQL function
#29: With a provider of transformations, I can take queries in C# and turn them into queries against a bunch of different data sources. .NET ships with a few, but you can write others.
#36: Like fibers in a thread, binding with SelectMany takes many sets and combines them into one.
#37: With Select, this would look like { {Andys orders}, {Beths orders}, {Charlies orders}}
With SelectMany, this would look like { Andy1, Andy2, Beth1, Charlie1, Charlie2}
#39: SelectMany is like doing a cross join in SQL, whereas Join is an inner join
#41: Easy to Read is query comprehension syntax. It came from the Entity Framework folks that were also working on this data query problem.
Hard to Read is extension methods.
#42: Skip and Take allow you to page through your data. Theres also SkipWhile and TakeWhile.