1 / 47

LINQ, An Intro

LINQ, An Intro. Florin−Tudor Cristea, Microsoft Student Partner. Hello, LINQ!. Because “Hello, world!” is so out of fashion. Inspiration sources http://students.info.uaic.ro/ ~alexandru.stefan/ materiale/ LINQ.pdf http://linqinaction.net http://msdn.microsoft.com/ en-us/

moesha
Download Presentation

LINQ, An Intro

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. LINQ, An Intro Florin−Tudor Cristea, Microsoft Student Partner

  2. Hello, LINQ! Because “Hello, world!” is so out of fashion.

  3. Inspiration sources • http://students.info.uaic.ro/ • ~alexandru.stefan/ • materiale/ • LINQ.pdf • http://linqinaction.net • http://msdn.microsoft.com/ • en-us/ • vcsharp/ • aa336746.aspx

  4. Language INtegrated Query

  5. Software is simple. It boils down to two things: code and data. Writing software is not so simple, and one of the major activities it involves is writing code that deals with data.

  6. LINQ could be the missing link—whether this pun is intended is yet to be discovered—between the data world and the world of general-purpose programming languages.

  7. LINQ unifies data access, whatever the source of data, and allows mixing data from different kind of sources. It allows for query and set operations, similar to what SQL statements offer for databases.

  8. LINQ was designed to be used against any type of object or data source and to provide a consistent programming model for doing so.

  9. http://phplinq.codeplex.com http://jslinq.codeplex.com http://quaere.codehaus.org

  10. Once you learn how to use LINQ against an array or a collection, you also know most of the concepts needed to take advantage of LINQ with a database or an XML file.

  11. HelloLINQ.csproj hello linq world Words of length 9 beautiful wonderful Words of length 5 hello world Words of length 4 linq Total number of chars is 32 (an average of 6,4 per word)

  12. Hello, um… language enhancements! “How much wood could a woodchuck chuck if a woodchuck could chuck wood?” or what makes LINQ tick.

  13. Implicitly typed local variables, which permit the types of local variables to be inferred from the expressions used to initialize them.

  14. Object initializers, which ease construction and initialization of objects.

  15. Lambda expressions, an evolution of anonymous methods that provides improved type inference and conversion to both delegate types and expression trees.

  16. Extension methods, which make it possible to extend existing types and constructed types with additional methods. With extension methods, types aren’t extended but look as if they were.

  17. Anonymous types, which are types automatically inferred and created from object initializers.

  18. HelloLE.csproj HelloLEContd.csproj ExtensionMethodsDiscoverability.csproj

  19. LINQ building blocks Warning, headsplosions ahead!

  20. static void DisplayProcesses() { var processes = Process.GetProcesses() .Where(process => process.WorkingSet64 > 20*1024*1024) .OrderByDescending(process => process.WorkingSet64) .Select(process => new { process.Id, Name=process.ProcessName }); ObjectDumper.Write(processes); }

  21. sequences • query expressions • query operators • deferred query exec. • expression trees

  22. static void DisplayProcesses() { var processes = Process.GetProcesses() .Where(process => process.WorkingSet64 > 20*1024*1024) .OrderByDescending(process => process.WorkingSet64) .Select(process => new { process.Id, Name=process.ProcessName }); ObjectDumper.Write(processes); }

  23. The GetProcesses method of the System.Diagnostics.Process class returns an array of Process objects. This is not surprising and probably wouldn’t be interesting, except that arrays implement the generic IEnumerable<T> interface. This interface, which appeared with .NET 2.0, is key to LINQ. In our particular case, an array of Process objects implements IEnumerable<Process>.

  24. The IEnumerable<T> interface is important because Where, OrderByDescending, Select, and other standard query operators used in LINQ queries expect an object of this type as a parameter.

  25. public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source, Func<TSource, Boolean> predicate) { foreach (TSource element in source) { if (predicate(element)) yield return element; } } // System.Linq.Enumerable

  26. An iterator is an object that allows you to traverse through a collection’s elements. What is named an iterator in .NET is also known as a generator in other languages such as Python, or sometimes a cursor, especially within the context of a database.

  27. An iterator is easy to create: it’s simply a method that returns an enumeration and uses yield return to provide the values. Iterator.csproj

  28. LINQ queries rely heavily on lazy evaluation (deferred query execution). This is one of the most important concepts in LINQ. Without this facility, LINQ would perform very poorly. DeferredQueryExecution.csproj

  29. Query operators are not a language extension per se, but an extension to the .NET FCL. Query operators are a set of extension methods that perform operations in the context of LINQ queries. They are the real elements that make LINQ possible.

  30. static void DisplayProcesses() { var processes = Process.GetProcesses() .Where(process => process.WorkingSet64 > 20*1024*1024) .OrderByDescending(process => process.WorkingSet64) .Select(process => new { process.Id, Name=process.ProcessName }); ObjectDumper.Write(processes); } Call to Where

  31. public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source, Func<TSource, Boolean> predicate) { foreach (TSource element in source) { if (predicate(element)) yield return element; } } foreach loop Filter source Return elements

  32. Some intermediate operations (such as sorting and grouping) do require the entire source be iterated over. Our OrderByDescending call is an example of this.

  33. We’ve stressed several characteristics of extension methods (Where, etc.): • They work on enumerations. • They allow pipelined data processing. • They rely on delayed execution. • All these features make these methods useful to write queries. This explains why these methods are called query operators.

  34. var processes = Process.GetProcesses() .Where(process => process.WorkingSet64 > 20*1024*1024) .OrderByDescending(process => process.WorkingSet64) .Select(process => new { process.Id, Name=process.ProcessName });

  35. var processes = from process in Process.GetProcesses() where process.WorkingSet64 > 20*1024*1024 orderby process.WorkingSet64 descending select new { process.Id, Name=process.ProcessName };

  36. The two code pieces are semantically identical. A query expression is convenient declarative shorthand for code you could write manually. Query expressions allow us to use the power of query operators, but with a query-oriented syntax.

  37. When you use a query expression, the compiler automagically translates it into calls to standard query operators.

  38. fromidinsource { fromidinsource | joinidinsourceonexprequalsexpr [ intoid ] | letid = expr | wherecondition | orderbyordering, ordering, … } selectexpr | groupexprbykey [ intoidquery ] Starts with from Zero or more from, join, let, where, or orderby Use let to declare temporary variables Ends with selector group by Optional into continuation

  39. var authors = from distinctAuthor in ( from book in SampleData.Books where book.Title.Contains("LINQ") from author in book.Authors.Take(1) select author) .Distinct() select new {distinctAuthor.FirstName, distinctAuthor.LastName};

  40. var authors = SampleData.Books .Where(book => book.Title.Contains("LINQ")) .SelectMany(book => book.Authors.Take(1)) .Distinct() .Select(author => new {author.FirstName, author.LastName});

  41. As for expression trees... we’ll cover these later (deferred query execution redux, IQueryable, dynamic queries, abracadabra). Maybe.

  42. System.Core.dll • System • System.Linq • System.Linq.Expressions • System.Data.DataSetExtensions.dll • System.Data • System.Data.Linq.dll • System.Data.Linq • System.Data.Linq.Mapping • System.Data.Linq.SqlClient • System.Xml.Linq.dll • System.Xml.Linq • System.Xml.Schema • System.Xml.XPath

  43. See y’all next time!

More Related