1 / 90

Language Integrated Query in .NET (LINQ)

Language Integrated Query in .NET (LINQ). Extension Methods, Lambda Expressions, LINQ Operators, Expressions, Projections, Aggregations. http://schoolacademy.telerik.com. Svetlin Nakov. Telerik Corporation. www.telerik.com. Table of Contents. Extension Methods Anonymous Types

zion
Download Presentation

Language Integrated Query in .NET (LINQ)

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. Language Integrated Query in .NET (LINQ) Extension Methods, Lambda Expressions, LINQ Operators, Expressions, Projections, Aggregations http://schoolacademy.telerik.com Svetlin Nakov Telerik Corporation www.telerik.com

  2. Table of Contents • Extension Methods • Anonymous Types • Lambda Expressions • LINQ Building Blocks • Query Operators and Expressions • Query Expression Trees • LINQ to Objects: Querying Collections • Projection, Conversion, Aggregation • Sorting, Grouping, Joins, Nested Queries

  3. Extension Methods

  4. Extension Methods • Once a type is defined and compiled into an assembly its definition is, more or less, final • The only way to update, remove or add new members is to recode and recompile the code • Extension methods allow existing compiled types to gain new functionality • Without recompilation • Without touching theoriginal assembly

  5. Defining Extension Methods • Extension methods • Defined in a static class • Defined as static • Use thiskeyword before its first to specify the class to be extended • Extension methods are "attached" to the extended class • Can also be called from statically through the defining static class

  6. Extension Methods – Examples public static class Extensions { public static int WordCount(thisstring str) { return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length; } } ... static void Main() { string s = "Hello Extension Methods"; int i = s.WordCount(); Console.WriteLine(i); }

  7. Extension Methods – Examples (2) public static void IncreaseWidth( this IList<int> list, int amount) { for (int i = 0; i < list.Count; i++) { list[i] += amount; } } ... static void Main() { List<int> ints = new List<int> { 1, 2, 3, 4, 5 }; ints.IncreaseWidth(5); // 6, 7, 8, 9, 10 }

  8. Extension Methods Live Demo

  9. Anonymous Types

  10. Anonymous Types • Anonymous types • Encapsulate a set of read-only properties and their value into a single object • No need to explicitly define a type first • To define an anonymous type • Use of the new var keyword in conjunction with the object initialization syntax var point = new { X = 3, Y = 5 };

  11. Anonymous Types – Example • At compile time, the C# compiler will autogeneratean uniquely named class • The class name is not visible from C# • Using implicit typing (var keyword) is mandatory // Use an anonymous type representing a car var myCar = new { Color = "Red", Brand = "BMW", Speed = 180 }; Console.WriteLine("My car is a {0} {1}.", myCar.Color, myCar.Brand);

  12. Anonymous Types – Properties • Anonymous types are reference types directly derived from System.Object • Have overridden version of Equals(), GetHashCode(), and ToString() • Do not have == and != operators overloaded var p = new { X = 3, Y = 5 }; var q = new { X = 3, Y = 5 }; Console.WriteLine(p == q); // false Console.WriteLine(p.Equals(q)); // true

  13. Arrays of Anonymous Types • You can define and use arrays of anonymous types through the following syntax: var arr = new[] { new { X = 3, Y = 5 }, new { X = 1, Y = 2 }, new { X = 0, Y = 7 } }; foreach (var item in arr) { Console.WriteLine("({0}, {1})", item.X, item.Y); }

  14. Anonymous Types Live Demo

  15. Lambda Expressions

  16. Lambda Expressions • A lambda expression is an anonymous function containing expressions and statements • Used to create delegates or expression tree types • All lambda expressions use the lambda operator =>, which is read as "goes to" • The left side of the lambda operator specifies the input parameters • The right side holds the expression or statement

  17. Lambda Expressions – Examples • Usually used with collection extension methods like FindAll() and Count() List<int> list = new List<int>() { 1, 2, 3, 4 }; List<int> evenNumbers = list.FindAll(x => (x % 2) == 0); foreach (var num in evenNumbers) { Console.Write("{0} ", num); } Console.WriteLine(); // 2 4 list.RemoveAll(x => x > 3); // 1 2 3

  18. Sorting with Lambda Expression var pets = new Pet[] { new Pet { Name="Sharo", Age=8 }, new Pet { Name="Rex", Age=4 }, new Pet { Name="Strela", Age=1 }, new Pet { Name="Bora", Age=3 } }; var sortedPets = pets.OrderBy(pet => pet.Age); foreach (Pet pet in sortedPets) { Console.WriteLine("{0} -> {1}", pet.Name, pet.Age); }

  19. Lambda Code Expressions • Lambda code expressions: List<int> list = new List<int>() { 20, 1, 4, 8, 9, 44 }; // Process each argument with codestatements List<int> evenNumbers = list.FindAll((i) => { Console.WriteLine("value of i is: {0}", i); return (i % 2) == 0; }); Console.WriteLine("Your even numbers are:"); foreach (int even in evenNumbers) Console.Write("{0}\t", even);

  20. Delegates Holding Lambda Functions • Lambda functions can be stored in variables of type delegate • Delegates are typed references to functions • Standard function delegates in .NET: • Func<TResult>, Func<T,TResult>, Func<T1,T2,TResult>, … Func<bool> boolFunc = () => true; Func<int, bool> intFunc = (x) => x < 10; if (boolFunc() && intFunc(5)) Console.WriteLine("5 < 10");

  21. Lambda Expressions Live Demo

  22. LINQ and Query Keywords

  23. LINQ and Query Keywords • Language Integrated Query (LINQ) query keywords • from – specifies data source and range variable • where – filters source elements • select – specifies the type and shape that the elements in the returned sequence • group– groups query results according to a specified key value • orderby – sorts query results in ascending or descending order

  24. Query Keywords – Examples • select, fromand where clauses: int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var querySmallNums = from num in numbers where num < 5 select num; foreach (var num in querySmallNums) { Console.Write(num.ToString() + " "); } //The result is 4 1 3 2 0

  25. Query Keywords – Examples (2) • Nested queries: string[] towns = { "Sofia", "Varna", "Pleven", "Ruse", "Bourgas" }; var townPairs = from t1 in towns from t2 in towns select new { T1 = t1, T2 = t2 }; foreach (var townPair in townPairs) { Console.WriteLine("({0}, {1})", townPair.T1, townPair.T2); }

  26. Query Keywords – Examples (3) • Sorting with оrderby: string[] fruits = { "cherry", "apple", "blueberry", "banana" }; // Sort in ascending sort var fruitsAscending = from fruit in fruits orderby fruit select fruit; foreach (string fruit in fruitsAscending) { Console.WriteLine(fruit); }

  27. LINQ Query Keywords Live Demo

  28. LINQ Building Blocks

  29. LINQ Building Blocks • Software developers spend a lot of time to obtain and manipulate data • Data can be stored in • Collections • Databases • XML documents • etc... • As of .NET 3.5 developers can use LINQ – a simplified approach to data manipulation

  30. LINQ Building Blocks (2) • LINQ is a set of extensions to .NET Framework • Encompasses language-integrated query, set, and transform operations • Consistent manner to obtain and manipulate "data" in the broad sense of the term • Queryexpressionscan be defined directly within the C# programming language • Used to interact with numerous data types • Converted to expression trees at compile time and evaluated at runtime

  31. LINQ Building Blocks (3) • LINQ allows query expressions to manipulate: • Any object implementing IEnumerable<T> • Collections of objects • Relational databases • XML documents • The query expressions are based on numerous SQL-like query operators • Intentionally designed to look and feel very similar to SQL expressions

  32. LINQ Building Blocks (4) • "LINQ" is the term used to describe this overall approach to data access • LINQ to Objects • LINQ over objects implementing IEnumerable<T> • LINQ to SQL and LINQ to Entities implement LINQ over relational data • LINQ to DataSetis a superset of LINQ to SQL • LINQ to XML is LINQ over XML documents

  33. LINQ to * Others … • VB.NET C# .NET Language-Integrated Query (LINQ) LINQ enabled data sources LINQ enabled ADO.NET LINQ to XML LINQ to Objects LINQ to DataSets LINQ to Entities LINQ to SQL <book> <title/> <author/> <price/> </book> Objects XML Relational Data

  34. Query Operations • All LINQ query operations consist of three distinct actions: • Obtain the data source • Create the query • Execute the query

  35. LINQ Sequences

  36. IEnumerable<T> and Sequences • The interface IEnumerable<T> is universal LINQ data source • Implemented by arrays and all .NET generic collections • Enables enumerating over a collection of elements • A sequencein LINQ means a collection implementing the IEnumerable<T>interface • Any variable declared as IEnumerable<T> for type T is considered a sequence of type T

  37. IEnumerable<T> and Sequences (2) • Most of the Standard Query Operators are extension methods in the static class System.Linq.Enumerable • Prototyped with an IEnumerable<T> as their first argument • E.g. Min(IEnumerable<T>), Where(IEnumerable<T>,Func<T,bool>) • Use the Cast or OfType operators to perform LINQ queries on legacy, non-generic .NET collections

  38. Query Operators and Expressions

  39. LINQ Query Expressions • When you have a collection of data, a common task is to extract a subset of items based on a given requirement • You want to obtain only the items with names that contain a number • Or don’t have embedded spaces • LINQ query expressions can greatly simplify the process • Query expressions are written in a declarative query syntax introduced in C# 3.0

  40. LINQ Query Expressions (2) • LINQ query expressions are written in a declarative SQL-like syntax • Example: extracting a subset of array containing items with names of more than 6 characters: string[] games ={"Morrowind", "BioShock","Daxter", "The Darkness","Half Life", "System Shock 2"}; IEnumerable<string> subset = from g in games where g.Length > 6 orderby g select g; foreach (string s in subset) Console.WriteLine("Item: {0}", s);

  41. Query Expressions Live Demo

  42. LINQ Query Expressions (3) • In LINQ a query is a basic language construction • Just like classes, methods and delegates in C# • Query expressions are used to query and transform data from any LINQ-enabled data source • A LINQ query is not executed until • You iterate over the query results • You try to access any of the elements in the result set

  43. Query Operators and the LINQ Extension Methods • Query operators in C# are keywords like: • from, in, where, orderby, select, … • For each standard query operator a corresponding extension method exists • E.g. where Where(IEnumerable<T>) • At compile time the C# compiler translates query expressions into expression trees • Expression trees are sequences of method calls (from System.Linq.Enumerable)

  44. Query Operators – Syntax • The basic syntax of LINQ queries is: • This selects all elements in games data source • You can apply criteria by the operator where • Any valid C# boolean expression can be used IEnumerable<string> subset = from g in games select g; var subset = games.Select(g => g); IEnumerable<string> subset = from g in games where g.Price < 20 select g; var subset = games.Select(g => g). Where(g => g.Price < 20);

  45. Query Operators (2) • Two sets of LINQ standard operators • Operating on IEnumerable<T> • Operating on IQueryable<T> • LINQ query operators are shorthand versions for various extension methods • Defined in System.Linq.Enumerabletype • Example: var subset = from g in games where g.Price < 20 select g; IEnumerable<string> subset = games.Where(g => g.Price < 20);

  46. Query Operators (3) • The standard query operators provide query capabilities including • Filtering – where • Projection – select, selectMany • Aggregation – Sum, Max, Count, Average • Sorting – orderby • Grouping – groupby • … and many more

  47. Standard Query Operators – Example string[] games = {"Morrowind", "BioShock","Half Life", "The Darkness","Daxter", "System Shock 2"}; // Build a query expression using extension methods // granted to the Array via the Enumerable type var subset = games.Where(game => game.Length >6). OrderBy(game => game).Select(game => game); foreach (var game in subset) Console.WriteLine(game); Console.WriteLine(); var subset = from g in games where g.Length > 6 orderby g select g;

  48. Standard Query Operators Live Demo

  49. Query Expression Trees

  50. Query Expression Trees • A queryexpression tree is an efficient data structure representing a LINQ expression • Type of abstract syntax tree used for storing parsed expressions from the source code • Lambda expressions often translate into query expression trees • IQueryable<T>is interface implemented by query providers (e.g. LINQ to SQL, LINQ to XML, LINQ to Entities) • IQueryable<T> objects use expression trees

More Related