1 / 20

Introduction to LINQ and Lambda Expressions

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

cortez
Download Presentation

Introduction to LINQ and Lambda Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Presented by: Andrew Gray Introduction to LINQand 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) 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 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... 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 (fromDataRow r intable.Rows where r[field].ToString() == value select r).ToArray(); } 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 (fromDataRow r intable.Rows.AsParallel().WithDegreeOfParallelism(2) where r[field].ToString() == value select r).ToArray(); } 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 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? 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 => { intb = 7; returna + b; } • As with delegates, anonymous methods are called by using the variable with the given parameters. • returnfoo(3); • The definition is not executed until it is called! 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. 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(inta = 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 Expressions

  12. Using Lambda Expressions public class CoinExample { private const string _Heads = “Heads!”; private const string _Tails = “Tails!”; privateFunc<string> _Flip; privateFunc<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()); } } 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>) privatestaticDataRowFoo(DataTable table, stringfindMe) { if (table == null || table.Rows.Count == 0) throw new ArgumentNullException(“table”); DataRow query = table.Where(a => a == findMe).GetFirstOrDefault(); return query; } 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 inta = 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”); } 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” ); } Introduction to LINQ and Lambda Expressions

  16. Using Lambda Expressions • Lambda Expressions can help implement dependency injection. public classActionInjectionExample { private decimal _Primary; private decimal _Alt; public boolIsGreater { getBinaryCompare(() => _Primary > _Alt); } public boolIsLesser { getBinaryCompare(() => _Primary < _Alt); } public boolBinaryCompare(Func<bool> action) { // Implementation details… boolresult = action(); // Further details… return result; } } 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] Introduction to LINQ and Lambda Expressions

  18. Open Discussion • Questions? • Past Experience? Introduction to LINQ and Lambda Expressions

  19. References 1http://msdn.microsoft.com/en-us/library/bb397926.aspx (Introduction to LINQ) 2http://msdn.microsoft.com/en-us/library/bb397906.aspx (LINQ) 3http://stackoverflow.com/questions/1524813/convert-this-linq-expression-into-lambda (LINQ example) 4http://msdn.microsoft.com/en-us/library/dd997425.aspx (Introduction to PLINQ) 5http://en.wikipedia.org/wiki/Anonymous_function (Anonymous Function/Lambda Expression) 6http://en.wikipedia.org/wiki/Closure_(computer_science) (Lexical Closures) 7http://msdn.microsoft.com/en-us/library/bb397687.aspx (Lambda Expressions - MSDN) 8http://alookonthecode.blogspot.com/2011/05/lambda-expressions-anonymous-classes.html (Issues) 9http://www.h3mm3.com/2011/06/unsubscribing-to-events-in-c.html (Unsubscribing from Lambdas) 10http://msdn.microsoft.com/en-us/library/ms228360(v=vs.90).aspx (Passing Primitives / Classes) Introduction to LINQ and Lambda Expressions

  20. Fin. Thank you for attending! andrew.russel.gray@gmail.com Introduction to LINQ and Lambda Expressions

More Related