Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Slides license: Creative Commons Attribution Non-Commercial Share Alike See
LINQ and Collections An introduction to LINQ and Collections.
An Introduction to the Reactive Extensions Ivan Towlson Mindscape.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
James Kolpack, InRAD LLC popcyclical.com. CodeStock is proudly partnered with: Send instant feedback on this session via Twitter: Send a direct message.
C. Varela; Adapted from S. Haridi and P. Van Roy1 Declarative Programming Techniques Lazy Execution (VRH 4.5) Carlos Varela RPI Adapted with permission.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
C# and LINQ Yuan Yu Microsoft Research Silicon Valley.
Presented by: Andrew Gray 1Introduction to LINQ and Lambda Expressions.
Threads in C# Threads in C#.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Iterator Pattern Dr. Neal CIS 480. Iterator An iterator pattern can be used when one class is a collection of things and would like to provide a standardized.
2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.
Dr. Muhammed Al-Mulhem ICS An Introduction to Functional Programming.
PARALLEL PROGRAMMING ABSTRACTIONS 6/16/2010 Parallel Programming Abstractions 1.
LINQ Programming in C# LINQ CSE Prof. Roger Crawfis.
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
LINQ, An IntroLINQ, An Intro Florin−Tudor Cristea, Microsoft Student Partner.
Eric Vogel Software Developer A.J. Boggs & Company.
High-Level Programming Languages: C++
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
Computing with C# and the.NET Framework Chapter 1 An Introduction to Computing with C# ©2003, 2011 Art Gittleman.
Putting it all together: LINQ as an Example. The Problem: SQL in Code Programs often connect to database servers. Database servers only “speak” SQL. Programs.
References types and Value types With a very brief introduction to struct and enum Reference types and Value types1.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Based on material from Telerik Corporation.
Monads Steve Goguen. About Me Web Developer for Allied Building Supply  ASP.NET, SQL Server, LINQ, etc. Website:
Task Parallel Library (TPL)
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
 Although VERY commonly used, arrays have limited capabilities  A List is similar to an array but provides additional functionality, such as dynamic.
Introduction to LINQ Chapter 11. Introduction Large amounts of data are often stored in a database—an organized collection of data. A database management.
C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
1-1 An Introduction to Functional Programming Sept
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
LINQ Language Integrated Query LINQ1. LINQ: Why and what? Problem Many data sources: Relational databases, XML, in-memory data structures, objects, etc.
LINQ and Lambda Expressions Telerik Software Academy LINQ Overview.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Chapter 11.  Large amounts of data are often stored in a database—an organized collection of data.  A database management system (DBMS) provides mechanisms.
Functional Processing of Collections (Advanced) 6.0.
Introduction to.NET Florin Olariu “Alexandru Ioan Cuza”, University of Iai Department of Computer Science.
Functional Programming
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming and Stream API
Critical sections, locking, monitors, etc.
More Functional Programming
Delegates/ Anders Børjesson
Functional Processing of Collections (Advanced)
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
Introduction to LINQ Chapter 11 10/28/2015 Lect 4 CT1411.
FP Foundations, Scheme In Text: Chapter 14.
Introduction to LINQ Chapter 11.
Generics in C# / Anders Børjesson
High Level Programming Languages
CHAPTER 21 LOOPS 1.
CS4540 Special Topics in Web Development LINQ to Objects
Presentation transcript:

Inside LINQ to Objects How LINQ to Objects work Inside LINQ1

IEnumerable and IEnumerator Two important interfaces in LINQ to Objects IEnumerable is a very simple, single method interface Interface IEnumerable { IEnumerator GetEnumerator() } IEnumerable is implemented by a lot of classes List, Array, HashSet etc. A LINQ expression returns a IEnumerable object IEnumerable is used in foreach statements IEnumerator is a slightly more complex interface: It has 3 methods Interface IEnumerator { bool MoveNext(); T Current(); void Reset(); } Inside LINQ2

Why do we need two interfaces? IEnumerator is a “machine” that can visit all the elements in a collection. A collection implements IEnumerable A “machine” factory! A collection can have any number of IEnumerator objects working on it. Each IEnumerator object has its own Current property. Source: Bart De Smet, C# 5.0 Unleashed, page 979 Inside LINQ3

LINQ to Objects Extension Methods LINQ to Objects queries are translated into LINQ to Objects method expression syntax var query = from x in xs where x % 2 == 0 select x + 1; Is translated to xs.Where(x => x % 2 == 0).Select(x => x +1); Where and Select are so-called extension methods. They work on IEnumerable objects (input) and return IEnumerable object (output) Makes is possible to chain method calls (like above) However, Where and Select are not methods defined in the IEnumerable interface They are special extension methods They extends IEnumerable … Examples: ExtensionMethods Inside LINQ4

Extension Methods: Technical requirements Extension Methods can be define on any class Not just IEnumerable Example: String.Reverse() ExtensionMethods An Extension Method must be declared inside a static class static class SomeName { … } You cannot create objects from a static class An Extension Method must be static The first parameter of an Extension Method must be marked this public static String Reverse(this String str) Inside LINQ5

Unit testing LINQ LINQ statements can be unit tested using the extension method SequenceEquals(…) It returns true if two LINQ statements returns the same sequence of elements Assert.IsTrue(lint.SequenceEquals(expected)); Example: linqFirstTry LINQ6

Iterators, yield return Implementing the interfaces IEnumerable and IEnumerator requires quite a lot of (boring) programming But there is a easier way … C# introduced some syntactic sugar to help you writing fewer lines Example IEnumerable GetIntegers() { for (int i = 0; i < 100; i++) { yield return i; } Iterators can consume other iterators Pulling elements through a chain of filters Example: PasswordCrackerLinq Inside LINQ7

Iterators are lazy Iterators returns an element only when they are asked to Elements are pulled through the chain of iterators Example: PasswordCrackingLinq Checking parameters is lazy, as well They are not checked until the first call to MoveNext() Example: PasswordCrackingLinq -> Variations(…) Eager methods include ToList(), ToArray(), Min(), etc. Inside LINQ8

PLINQ = Parallel LINQ LINQ statements can be executed concurrently AsParallel() Another iterator Executes the FOLLOWING iterator in parallel PLINQ decides how many threads will be used Examples: PlinqTrying Inside LINQ9

Functional programming A programming paradigm Computation = evaluation of mathematical functions No state No side effects Programming languages LISP Haskell F# C# lambda expressions + LINQ (may include side effects) Inside LINQ10

References and further readings Bart De Smet: C# 5.0 Unleashed, Sams 2013 Chapter 10 Methods: section Extension Methods, page Chapter 20 Language Integrated Query Internals, page MSDN Extension Methods (C# Programming Guide) MSDN Parallel LINQ (PLINQ) Inside LINQ11