45
Chuck Durfee, 3 October 2012

Linq - an overview

Embed Size (px)

Citation preview

Page 1: Linq - an overview

Chuck Durfee, 3 October 2012

Page 2: Linq - an overview
Page 3: Linq - an overview
Page 4: Linq - an overview

BIGDATA

Page 5: Linq - an overview
Page 6: Linq - an overview
Page 7: Linq - an overview
Page 8: Linq - an overview

Enumerable.Select( Enumerable.OrderBy( Enumerable.Where(names, s => s.Length == 5), s => s ), s => s.ToUpper());

versusnames.Where(s => s.Length == 5) .OrderBy(s => s) .Select(s => s.ToUpper())

Page 9: Linq - an overview
Page 10: Linq - an overview

var, my dear Watson

Page 11: Linq - an overview
Page 12: Linq - an overview

1.0delegate bool FilterMethod(string s);

private bool IsFiveLetterName(string s) { return s.Length == 5;}

public DotNet10Land(){ FilterMethod filter = IsFiveLetterName;}

Page 13: Linq - an overview

delegate bool FilterMethod(string s);

public DotNet20Land(){ FilterMethod filter = delegate(string s) { return s.Length == 5; };}

2.0

Page 14: Linq - an overview

C# 3 = .NET 3.5delegate bool FilterMethod(string s);

public DotNet35Land(){ string chuck = "chuck"; FilterMethod filter = delegate(string s) { return s != chuck && s.Length == 5; };}

Page 15: Linq - an overview
Page 16: Linq - an overview

Func// delegate bool FilterMethod(string s);

public DotNet35Land(){ Func<string, bool> filter = delegate(string s) { return s.Length == 5; };}

Page 17: Linq - an overview

Func<string, bool>Func<string, int, DateTime>Func<List<string>, int, bool>

but not

Func<string, System.Void>

Page 18: Linq - an overview

// delegate void ActionMethod(string s);

public DotNet35Land(){

Action<string> action = delegate(string s) { if (s.Length != 5) throw new ArgumentException("Length != 5");};

}

and… Action

Page 19: Linq - an overview

LAMBDA EXPRESSIONSFunc<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;

Page 20: Linq - an overview

by Alonzo Church, 1930’s

Page 21: Linq - an overview

5

Page 22: Linq - an overview

() => 5

Page 23: Linq - an overview

λ(5)

Page 24: Linq - an overview

5 + 2 =

λ(5) + λ(2)

Page 25: Linq - an overview

λ(5) + λ(2)=

+(λ(5), λ(2))

Page 26: Linq - an overview
Page 27: Linq - an overview
Page 28: Linq - an overview
Page 29: Linq - an overview
Page 30: Linq - an overview
Page 31: Linq - an overview
Page 32: Linq - an overview

string[] names = { "Tom", "Dick", "Harry" };

names.Select((s, i) => (i + 1) + "=" + s);

1=Tom2=Dick3=Harry

Page 33: Linq - an overview
Page 34: Linq - an overview

int[] numbers = { 3, 5, 7 };string[] words = { "three", "five", "seven", "ignored" };

IEnumerable<string> zip = numbers.Zip(words, (n, w) => n + "=" + w);

3=three5=five7=seven

Page 35: Linq - an overview
Page 36: Linq - an overview

IEnumerable<Order> spanishOrders = customers .Where(c => c.Country == "Spain") .SelectMany(c => c.Orders);

Page 37: Linq - an overview
Page 38: Linq - an overview

var slowQuery =from c in customersfrom p in purchases where c.ID == p.CustomerIDselect c.Name + " bought a " + p.Description;

var fastQuery =from c in customersjoin p in purchases on c.ID equals p.CustomerIDselect c.Name + " bought a " + p.Description;

Page 39: Linq - an overview
Page 40: Linq - an overview

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));

Page 41: Linq - an overview
Page 42: Linq - an overview
Page 43: Linq - an overview

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

Page 44: Linq - an overview
Page 45: Linq - an overview

Chuck Durfeehttp://neontapir.com