Download presentation
Presentation is loading. Please wait.
Published byMitchel Strutton Modified over 10 years ago
1
Presented by: Andrew Gray 1Introduction 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.htmlhttp://andrewrgray.webs.com/games.html (Requires Unity Webplayer) Introduction to LINQ and Lambda Expressions2
3
Agenda What is LINQ? Using LINQ LINQ Considerations What is a Lambda Expression? Using Lambda Expressions Lambda Considerations Open Discussion References Conclusion 3Introduction 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... 4Introduction 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(); } 5Introduction 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(); } Introduction to LINQ and Lambda Expressions6
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 7Introduction 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]functionnon-local variables ○ No/Limited tail-call optimization in C# ○ Language syntax is limited inside a Lambda 8Introduction to LINQ and Lambda Expressions What do you call a baby eigensheep?
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 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! 9Introduction to LINQ and Lambda Expressions
10
Using Lambda Expressions Lambdas can be mapped to the first-class function ( Func ) 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 All parameters are inputs Actions have a void return type. Introduction to LINQ and Lambda Expressions10
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 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 Introduction to LINQ and Lambda Expressions11
12
Using Lambda Expressions public class CoinExample { private const string _Heads = “Heads!”; private const string _Tails = “Tails!”; private Func _Flip; private Func _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()); } 12Introduction to LINQ and Lambda Expressions
13
Using Lambda Expressions – IQueryable Lambda Expressions are often arguments in various IQueryable methods!.Where(Func ).Select(Func ) 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; } 13Introduction to LINQ and Lambda Expressions
14
Using Lambda Expressions Lambdas do not require arguments to refer to external variables Func _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”); } 14Introduction 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 _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” ); } 15Introduction 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 action) { // Implementation details… bool result = action(); // Further details… return result; } Introduction to LINQ and Lambda Expressions16
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] 17Introduction to LINQ and Lambda Expressions
18
Open Discussion Questions? Past Experience? 18Introduction to LINQ and Lambda Expressions
19
References 1 http://msdn.microsoft.com/en-us/library/bb397926.aspx (Introduction to LINQ)http://msdn.microsoft.com/en-us/library/bb397926.aspx 2 http://msdn.microsoft.com/en-us/library/bb397906.aspx (LINQ)http://msdn.microsoft.com/en-us/library/bb397906.aspx 3 http://stackoverflow.com/questions/1524813/convert-this-linq-expression-into-lambda (LINQ example) http://stackoverflow.com/questions/1524813/convert-this-linq-expression-into-lambda 4 http://msdn.microsoft.com/en-us/library/dd997425.aspx (Introduction to PLINQ)http://msdn.microsoft.com/en-us/library/dd997425.aspx 5 http://en.wikipedia.org/wiki/Anonymous_function (Anonymous Function/Lambda Expression)http://en.wikipedia.org/wiki/Anonymous_function 6 http://en.wikipedia.org/wiki/Closure_(computer_science) (Lexical Closures)http://en.wikipedia.org/wiki/Closure_(computer_science) 7 http://msdn.microsoft.com/en-us/library/bb397687.aspx (Lambda Expressions - MSDN)http://msdn.microsoft.com/en-us/library/bb397687.aspx 8 http://alookonthecode.blogspot.com/2011/05/lambda-expressions-anonymous-classes.html (Issues)http://alookonthecode.blogspot.com/2011/05/lambda-expressions-anonymous-classes.html 9 http://www.h3mm3.com/2011/06/unsubscribing-to-events-in-c.html (Unsubscribing from Lambdas)http://www.h3mm3.com/2011/06/unsubscribing-to-events-in-c.html 10 http://msdn.microsoft.com/en-us/library/ms228360(v=vs.90).aspx (Passing Primitives / Classes)http://msdn.microsoft.com/en-us/library/ms228360(v=vs.90).aspx 19Introduction to LINQ and Lambda Expressions
20
Fin. Thank you for attending! andrew.russel.gray@gmail.com 20Introduction to LINQ and Lambda Expressions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.