introduction to linq and lambda expressions

20
INTRODUCTION TO LINQ AND LAMBDA EXPRESSIONS Presented by: Andrew Gray 1 Introduction to LINQ and Lambda Expressions

Upload: travis-hodges

Post on 01-Jan-2016

28 views

Category:

Documents


10 download

DESCRIPTION

Presented by: Andrew Gray. Introduction to LINQ and Lambda Expressions. About Me. Andrew Gray Developer Current Work: Technical Test Engineer at McLane Advanced Technologies ( Ranorex Studio/C#) Hobbies: Music, Gaming, Indie Game Development Game Engines: Torque, Unity - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 1

INTRODUCTION TO LINQ

AND LAMBDA EXPRESSIONS

Presented by:

Andrew Gray

Page 2: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 2

About Me

Andrew Gray Developer Current Work: Technical Test Engineer at McLane Advanced

Technologies (Ranorex Studio/C#) Hobbies: Music, Gaming, Indie Game Development Game Engines: Torque, Unity

Previous Works: http://andrewrgray.webs.com/games.html

(Requires Unity Webplayer)

Page 3: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 3

Agenda What is LINQ? Using LINQ LINQ Considerations What is a Lambda Expression? Using Lambda Expressions Lambda Considerations Open Discussion References Conclusion

Page 4: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 4

What is LINQ? Acronym – Language-Integrated Query [1] Purpose - Improvement in notation Introduced in .NET 3.5 / Visual Studio 2008 Provides SQL-like operations for searching

in-memory data [2] Often suggested by ReSharper when an

IEnumerable is iterated over in a loop.

No, not him...

Page 5: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 5

Using LINQ – Standard Form

Create Query [2] Execute the Query on a data source [3]

using System.Core;

// …Other Declarations…

private static DataRow[] FindRowsInTable(DataTable table, string field, string value){ return (from DataRow r in table.Rows where r[field].ToString() == value select r).ToArray();}

Page 6: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 6

Using LINQ - Parallelization

Parallelization in LINQ is achieved by adding .AsParallel() after the collection name. [4]

Parallel LINQ by default uses all cores up to 64, but can be limited by using .DegreeOfParallelism(int cores) [4]

Using PLINQ can yield a performance benefit for your application, as core usage can be controlled. [4]

using System.Core;

// …Other Declarations…

private static DataRow[] FindRowsInTable(DataTable table, string field, string value){ return (from DataRow r in table.Rows.AsParallel().WithDegreeOfParallelism(2) where r[field].ToString() == value select r).ToArray();}

Page 7: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 7

LINQ Considerations Syntactic Sugar (Replaces for/foreach loop with SQL-like syntax) Parallelization is possible – potential speed boost Query execution is deferred from declaration Additional methods have been added in the IQueryable interface Code Smell: LINQ query on information read from a database

A SQL query is generally better Some LINQ commands take Lambda Expressions as parameters

Page 8: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 8

What is a Lambda Expression? Origin – Anonymous Functions

Lambda Calculus (1936 – Alonzo Church) [5] LISP (1957) [4]

Purpose A refined way of defining anonymous methods or delegates Implements “closures” in C#

○ Closure: “A function together with a referencing environment for the non-local variables of that function.” [6]

○ No/Limited tail-call optimization in C#○ Language syntax is limited inside a Lambda

What do you call a baby eigensheep?

Page 9: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 9

Using Lambda Expressions

Lambdas take the form: (args[]) => { statements; } Formally, => (lambda operator) reads as ‘[Left] goes to [Right]’ [7] Lambdas accept a void parameter list.

() => statement(s); Lambdas do not require arguments to refer to external variables return isn’t required to get the result of a lambda, unless the value or

reference of an internal variable is being returned. Func<int, int> foo = a => { int b = 7; return a + b; }

As with delegates, anonymous methods are called by using the variable with the given parameters. return foo(3); The definition is not executed until it is called!

Page 10: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 10

Using Lambda Expressions

Lambdas can be mapped to the first-class function (Func<[types]>) generic type The last type in the generic is the return type; all types before that are

argument types. Provides a means of storing or returning Lambdas with strong typing

Lambdas can also be mapped to Action<[types]> All parameters are inputs Actions have a void return type.

Page 11: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 11

Using Lambda Expressions

Keywords break, continue, and goto are not supported in Lambdas. [7] The params keyword cannot be used in Func<> or the lambda’s

parameter list. params can be used with standard delegate declarations.

Optional parameters are not supported in Lambdas. Func<int> foo (int a = 7) => a*a;

Does not compile! Cannot enclose ref or out parameters from the parent scope. [7] Cannot be on the left side of is or as [7] Lambdas in C# cannot be recursive like a named method

Page 12: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 12

Using Lambda Expressionspublic class CoinExample{ private const string _Heads = “Heads!”; private const string _Tails = “Tails!”;

private Func<string> _Flip; private Func<string, string, string> _Tree; private void CoinFlipper() { Random generator = new Random(GetSeed());

_Flip = () => ((generator.Next() % 2) == 0) ? _Heads : _Tails; _Tree = (lhs, rhs) => lhs + “ x ” + rhs; Console.WriteLine(“Result: ” + _Tree(_Flip(),_Flip()); }}

Page 13: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 13

Using Lambda Expressions – IQueryable Lambda Expressions are often arguments in various IQueryable methods!

.Where(Func<bool>) .Select(Func<bool>)

private static DataRow Foo(DataTable table, string findMe){

if (table == null || table.Rows.Count == 0)throw new ArgumentNullException(“table”);

DataRow query = table.Where(a => a == findMe).GetFirstOrDefault();

return query;}

Page 14: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 14

Using Lambda Expressions

Lambdas do not require arguments to refer to external variables

Func<string> _Compare;

private void Foo(){ const int a = 7, b = 7;

// With Lambda Expression… _Compare = () => a == b ? "Yes" : "No"; Console.WriteLine("Does A match B? " + _Compare());

// Normal way… Console.WriteLine(“Does A match B? “ + a == b ? “Yes” : “No”);}

Page 15: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 15

Using Lambda Expressions

Lambdas can be modified after initial declaration, but ReSharper will show a warning for the modification!

Func<bool> _Compare;

private void Foo(int a, int b){

// Initial declaration_Compare = () => a == b;

Console.WriteLine( “Does A match B? “ + _Compare() ? “Yes” : “No” );

_Compare = () => a > b; // Warning here!

Console.WriteLine( “Is A greater than B? “ + _Compare() ? “Yes” : “No” );

}

Page 16: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 16

Using Lambda Expressions

Lambda Expressions can help implement dependency injection.

public class ActionInjectionExample{ private decimal _Primary; private decimal _Alt;

public bool IsGreater { get BinaryCompare(() => _Primary > _Alt); } public bool IsLesser { get BinaryCompare(() => _Primary < _Alt); }

public bool BinaryCompare(Func<bool> action) { // Implementation details… bool result = action(); // Further details…

return result; }}

Page 17: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 17

Lambda Considerations Pros

Provides support for first-class functions and expression trees Useful as arguments to certain LINQ query operations. Useful when a delegate will have a simple body. Lambdas can help implement dependency injection.

Neutral Lambdas defer execution until use.

Cons Unsubscribing a Lambda from an event requires additional work. [9] Variables used in a Lambda are not garbage collected until the

referencing delegate goes out of scope. [6] There is evidence that using local variables inside Lambdas can cause

resource leaks. [8]○ Variables that are not primitives are passed by reference. [10]

Page 18: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 18

Open Discussion Questions? Past Experience?

Page 19: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 19

References

1 http://msdn.microsoft.com/en-us/library/bb397926.aspx (Introduction to LINQ)

2 http://msdn.microsoft.com/en-us/library/bb397906.aspx (LINQ)

3 http://stackoverflow.com/questions/1524813/convert-this-linq-expression-into-lambda (LINQ example)

4 http://msdn.microsoft.com/en-us/library/dd997425.aspx (Introduction to PLINQ)

5 http://en.wikipedia.org/wiki/Anonymous_function (Anonymous Function/Lambda Expression)

6 http://en.wikipedia.org/wiki/Closure_(computer_science) (Lexical Closures)

7 http://msdn.microsoft.com/en-us/library/bb397687.aspx (Lambda Expressions - MSDN)

8 http://alookonthecode.blogspot.com/2011/05/lambda-expressions-anonymous-classes.html (Issues)

9 http://www.h3mm3.com/2011/06/unsubscribing-to-events-in-c.html (Unsubscribing from Lambdas)

10 http://msdn.microsoft.com/en-us/library/ms228360(v=vs.90).aspx (Passing Primitives / Classes)

Page 20: Introduction  to LINQ and Lambda Expressions

Introduction to LINQ and Lambda Expressions 20

Fin.Thank you for attending!

[email protected]