際際滷

際際滷Share a Scribd company logo
Chuck Durfee, 3 October 2012
LINQ
LINQ
BIG
DATA
LINQ
LINQ
LINQ
Enumerable.Select(
Enumerable.OrderBy(
Enumerable.Where(names, s => s.Length == 5),
s => s
),
s => s.ToUpper()
);
versus
names.Where(s => s.Length == 5)
.OrderBy(s => s)
.Select(s => s.ToUpper())
LINQ
, my dear Watson
LINQ
1.0
delegate bool FilterMethod(string s);
private bool IsFiveLetterName(string s) {
return s.Length == 5;
}
public DotNet10Land()
{
FilterMethod filter = IsFiveLetterName;
}
delegate bool FilterMethod(string s);
public DotNet20Land()
{
FilterMethod filter =
delegate(string s) {
return s.Length == 5;
};
}
2.0
C# 3 = .NET 3.5
delegate bool FilterMethod(string s);
public DotNet35Land()
{
string chuck = "chuck";
FilterMethod filter =
delegate(string s) {
return s != chuck && s.Length == 5;
};
}
LINQ
Func
// delegate bool FilterMethod(string s);
public DotNet35Land()
{
Func<string, bool> filter =
delegate(string s) {
return s.Length == 5;
};
}
Func<string, bool>
Func<string, int, DateTime>
Func<List<string>, int, bool>
but not
Func<string, System.Void>
// delegate void ActionMethod(string s);
public DotNet35Land()
{
Action<string> action = delegate(string s) {
if (s.Length != 5)
throw new ArgumentException("Length != 5");
};
}
and Action
LAMBDA EXPRESSIONS
Func<string, bool> filter =
delegate(string s) {return s.Length == 5;};
var filter = (string s) => {return s.Length == 5;};
var filter = (string s) => s.Length == 5;
var filter = (s) => s.Length == 5;
var filter = s => s.Length == 5;
by Alonzo Church, 1930s
LINQ
LINQ
LINQ
LINQ
LINQ
LINQ
LINQ
LINQ
LINQ
LINQ
LINQ
string[] names = { "Tom", "Dick",
"Harry" };
names.Select((s, i) =>
(i + 1) + "=" + s);
1=Tom
2=Dick
3=Harry
LINQ
int[] numbers = { 3, 5, 7 };
string[] words = { "three", "five",
"seven", "ignored" };
IEnumerable<string> zip =
numbers.Zip(words,
(n, w) => n + "=" + w);
3=three
5=five
7=seven
LINQ
IEnumerable<Order> spanishOrders =
customers
.Where(c => c.Country == "Spain")
.SelectMany(c => c.Orders);
LINQ
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;
LINQ
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)
);
LINQ
LINQ
public static class EnumerableExtensions
{
public static string ToCsv<T>(
this IEnumerable<T> sequence)
{
const string delimiter = ", ";
return sequence.Aggregate(
new StringBuilder(),
(sb, s) => sb.Append(s + delimiter),
sb => sb.ToString()
.TrimEnd(delimiter.ToArray()));
}
}
new[] { 1, 2, 3, 5, 8, 13, 20 } => 1, 2, 3, 5, 8, 13, 20
LINQ
LINQ

More Related Content

LINQ

  • 1. Chuck Durfee, 3 October 2012
  • 8. Enumerable.Select( Enumerable.OrderBy( Enumerable.Where(names, s => s.Length == 5), s => s ), s => s.ToUpper() ); versus names.Where(s => s.Length == 5) .OrderBy(s => s) .Select(s => s.ToUpper())
  • 10. , my dear Watson
  • 12. 1.0 delegate bool FilterMethod(string s); private bool IsFiveLetterName(string s) { return s.Length == 5; } public DotNet10Land() { FilterMethod filter = IsFiveLetterName; }
  • 13. delegate bool FilterMethod(string s); public DotNet20Land() { FilterMethod filter = delegate(string s) { return s.Length == 5; }; } 2.0
  • 14. C# 3 = .NET 3.5 delegate bool FilterMethod(string s); public DotNet35Land() { string chuck = "chuck"; FilterMethod filter = delegate(string s) { return s != chuck && s.Length == 5; }; }
  • 16. Func // delegate bool FilterMethod(string s); public DotNet35Land() { Func<string, bool> filter = delegate(string s) { return s.Length == 5; }; }
  • 17. Func<string, bool> Func<string, int, DateTime> Func<List<string>, int, bool> but not Func<string, System.Void>
  • 18. // delegate void ActionMethod(string s); public DotNet35Land() { Action<string> action = delegate(string s) { if (s.Length != 5) throw new ArgumentException("Length != 5"); }; } and Action
  • 19. LAMBDA EXPRESSIONS Func<string, bool> filter = delegate(string s) {return s.Length == 5;}; var filter = (string s) => {return s.Length == 5;}; var filter = (string s) => s.Length == 5; var filter = (s) => s.Length == 5; var filter = s => s.Length == 5;
  • 32. string[] names = { "Tom", "Dick", "Harry" }; names.Select((s, i) => (i + 1) + "=" + s); 1=Tom 2=Dick 3=Harry
  • 34. int[] numbers = { 3, 5, 7 }; string[] words = { "three", "five", "seven", "ignored" }; IEnumerable<string> zip = numbers.Zip(words, (n, w) => n + "=" + w); 3=three 5=five 7=seven
  • 36. IEnumerable<Order> spanishOrders = customers .Where(c => c.Country == "Spain") .SelectMany(c => c.Orders);
  • 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) );
  • 43. public static class EnumerableExtensions { public static string ToCsv<T>( this IEnumerable<T> sequence) { const string delimiter = ", "; return sequence.Aggregate( new StringBuilder(), (sb, s) => sb.Append(s + delimiter), sb => sb.ToString() .TrimEnd(delimiter.ToArray())); } } new[] { 1, 2, 3, 5, 8, 13, 20 } => 1, 2, 3, 5, 8, 13, 20

Editor's Notes

  • #3: Lets talk about the history of LINQ
  • #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
  • #13: http://www.paintingsoncanvas.net/print-111552-4043120/stone-age-man-making-weapon-giclee-print/
  • #15: http://www.travelwalls.net/wallpapers/2012/08/Walt-Disney-Concert-Hall-Modern-City-Los-Angeles-California-United-States-300x420.jpg
  • #17: Dont need to declare a delegate
  • #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.
  • #22: Take the number 5
  • #23: I could express that as a function that always returns 5
  • #24: This is more like Churchs notation
  • #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.
  • #30: Now to the practical side of things.
  • #31: Here, I use LINQPad (http://linqpad.com) to demonstrate Where and the like.
  • #32: Map or Project is a functional-language based description of the operation. We call it Select because of most C# programmers SQL heritage.
  • #33: Select can take a position argument. Many LINQ operators have overloads, check them out.
  • #35: Zip can replace simple Join statements.
  • #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.
  • #43: Examples include Count(), Sum()
  • #44: Here, I show the Aggregate command.
  • #45: Included in .NET 4.5 or can be downloaded as an external library in .NET 4 Use ToParallel() to invoke PLINQ
  • #46: Image: http://cdn.abovethelaw.com/uploads/2012/02/thank-you.jpg