SlideShare 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, 1930’s
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

PPTX
Linq - an overview
PDF
[WELC] 21. I’m Changing the Same Code All Over the Place
PPTX
Open course(programming languages) 20150121
PDF
Short intro to the Rust language
PDF
QGene Quiz 2016
PPT
Functions
PDF
Dimaggio sandler-v4
PPTX
Open course(programming languages) 20150304
Linq - an overview
[WELC] 21. I’m Changing the Same Code All Over the Place
Open course(programming languages) 20150121
Short intro to the Rust language
QGene Quiz 2016
Functions
Dimaggio sandler-v4
Open course(programming languages) 20150304

What's hot (15)

PPT
PDBC
PPTX
Python data structures
TXT
Card pack
PPTX
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
TXT
Card pack
PPTX
ETL for Pros: Getting Data Into MongoDB
TXT
Ass2 1 (2)
PDF
Perl6 a whistle stop tour
PPTX
Perl6 a whistle stop tour
ODP
DOC
Multiplicacion de polinomios 1ro
PDF
밑바닥부터 시작하는 의료 AI
PDF
The MongoDB Driver for F#
PDF
The Ring programming language version 1.5.3 book - Part 20 of 184
PDBC
Python data structures
Card pack
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Card pack
ETL for Pros: Getting Data Into MongoDB
Ass2 1 (2)
Perl6 a whistle stop tour
Perl6 a whistle stop tour
Multiplicacion de polinomios 1ro
밑바닥부터 시작하는 의료 AI
The MongoDB Driver for F#
The Ring programming language version 1.5.3 book - Part 20 of 184
Ad

Similar to LINQ (20)

PPT
Linq intro
PPT
PostThis
PPTX
PPTX
2. overview of c#
PDF
Dotnet programming concepts difference faqs- 2
PPT
Mixing functional and object oriented approaches to programming in C#
PPTX
Kdahlby 200908 Stldodn Linqinternals 090903222505 Phpapp01
PPTX
LINQ Internals - STLDODN
PPTX
Think in linq
PPTX
Linq Introduction
PPT
Understanding linq
PPT
Introduction to Linq
PPT
ASP.NET 08 - Data Binding And Representation
PPTX
PPTX
Linq Sanjay Vyas
PPTX
Mixing functional programming approaches in an object oriented language
PDF
C# Starter L04-Collections
PPT
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
PPT
PDF
Beginning linq
Linq intro
PostThis
2. overview of c#
Dotnet programming concepts difference faqs- 2
Mixing functional and object oriented approaches to programming in C#
Kdahlby 200908 Stldodn Linqinternals 090903222505 Phpapp01
LINQ Internals - STLDODN
Think in linq
Linq Introduction
Understanding linq
Introduction to Linq
ASP.NET 08 - Data Binding And Representation
Linq Sanjay Vyas
Mixing functional programming approaches in an object oriented language
C# Starter L04-Collections
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Beginning linq
Ad

Recently uploaded (20)

PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
A novel scalable deep ensemble learning framework for big data classification...
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
Chapter 5: Probability Theory and Statistics
PPTX
The various Industrial Revolutions .pptx
PPTX
1. Introduction to Computer Programming.pptx
PDF
Architecture types and enterprise applications.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Tartificialntelligence_presentation.pptx
PDF
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Developing a website for English-speaking practice to English as a foreign la...
Univ-Connecticut-ChatGPT-Presentaion.pdf
A comparative study of natural language inference in Swahili using monolingua...
Enhancing emotion recognition model for a student engagement use case through...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
A novel scalable deep ensemble learning framework for big data classification...
TLE Review Electricity (Electricity).pptx
Web App vs Mobile App What Should You Build First.pdf
Chapter 5: Probability Theory and Statistics
The various Industrial Revolutions .pptx
1. Introduction to Computer Programming.pptx
Architecture types and enterprise applications.pdf
Group 1 Presentation -Planning and Decision Making .pptx
1 - Historical Antecedents, Social Consideration.pdf
Getting Started with Data Integration: FME Form 101
Tartificialntelligence_presentation.pptx
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Developing a website for English-speaking practice to English as a foreign la...

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;
  • 20. by Alonzo Church, 1930’s
  • 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: Let’s 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, let’s 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: Don’t need to declare a delegate…
  • #18: System.Void isn’t 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 Church’s 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 that’s 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 { {Andy’s orders}, {Beth’s orders}, {Charlie’s 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. There’s 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