+ All Categories

LINQ

Date post: 17-Jul-2015
Category:
Upload: chuck-durfee
View: 63 times
Download: 0 times
Share this document with a friend
45
Chuck Durfee, 3 October 2012
Transcript
Page 1: LINQ

Chuck Durfee, 3 October 2012

Page 2: LINQ
Page 3: LINQ
Page 4: LINQ

BIGDATA

Page 5: LINQ
Page 6: LINQ
Page 7: LINQ
Page 8: 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())

Page 9: LINQ
Page 10: LINQ

, my dear Watson

Page 11: LINQ
Page 12: LINQ

1.0delegate bool FilterMethod(string s);

private bool IsFiveLetterName(string s) {

return s.Length == 5;

}

public DotNet10Land()

{

FilterMethod filter = IsFiveLetterName;

}

Page 13: LINQ

delegate bool FilterMethod(string s);

public DotNet20Land(){FilterMethod filter =

delegate(string s) {return s.Length == 5;

};}

2.0

Page 14: LINQ

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
Page 16: LINQ

Func// delegate bool FilterMethod(string s);

public DotNet35Land()

{

Func<string, bool> filter =

delegate(string s) {

return s.Length == 5;

};

}

Page 17: LINQ

Func<string, bool>

Func<string, int, DateTime>

Func<List<string>, int, bool>

but not

Func<string, System.Void>

Page 18: LINQ

// 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

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;

Page 20: LINQ

by Alonzo Church, 1930’s

Page 21: LINQ
Page 22: LINQ
Page 23: LINQ
Page 24: LINQ
Page 25: LINQ
Page 26: LINQ
Page 27: LINQ
Page 28: LINQ
Page 29: LINQ
Page 30: LINQ
Page 31: LINQ
Page 32: LINQ

string[] names = { "Tom", "Dick",

"Harry" };

names.Select((s, i) =>

(i + 1) + "=" + s);

1=Tom

2=Dick

3=Harry

Page 33: LINQ
Page 34: 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

Page 35: LINQ
Page 36: LINQ

IEnumerable<Order> spanishOrders = customers

.Where(c => c.Country == "Spain")

.SelectMany(c => c.Orders);

Page 37: LINQ
Page 38: 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;

Page 39: LINQ
Page 40: 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)

);

Page 41: LINQ
Page 42: LINQ
Page 43: 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

Page 44: LINQ
Page 45: LINQ

Recommended