76
LINQ Training 07/2013

LINQ

Embed Size (px)

DESCRIPTION

Guided Tour on LINQ for developers: basic and advanced concepts, examples

Citation preview

Page 1: LINQ

LINQ Training

07/2013

Page 2: LINQ

Agenda

A brief history…

What is LINQ ?

C# language enhancements

Base concepts

Advanced concepts

Examples

Going deeper?

Questions

2

Page 3: LINQ

I) A brief history…

Page 4: LINQ

ODBC & DAO

• ODBC (Open DataBase Connectivity): - released in 1992 by Microsoft, for Windows systems, based on the SQL

Access Group Specification

- Middleware dedicated to manipulate Databases

- Full software, with UI, API and drivers for all the SGBD on the market

- Latest version : 3.0 , released on 1999

• Data Access Object - API created to consume ODBC with VB

At this time, industry began to push « object-oriented, distributed, componentized software » : Microsoft needed to add a new data access for its COM response to Object Management Group’s CORBA.

4 For more info, see http://blogs.msdn.com/b/data/archive/2006/12/05/data-access-api-of-the-day-part-i.aspx

Page 5: LINQ

OLE DB & ADO

• Object Linking and Embedding - Designed to be the principal data access layer for the COM universe.

- Offers a « componentized » data access interface, able to work both with DBs (query result, table, view..) text files, Excel files, registery, email…

- Sources have a common data representation

- Low level interface => needs a wrapper API to be used : ADO

• ActiveX Data Oject - Use the same model than DAO, and add a Disconnected mode, base on a foreward

only cursor.

And then, MS started .NET framework project to counter Java…

5 For more info, see http://blogs.msdn.com/b/data/archive/2006/12/13/data-access-api-of-the-day-part-ii-componentizing-data-

access.aspx

Page 6: LINQ

ADO.NET

In 1998, « COM3 » project, that became .Net framework was launched.

With Internet and XML rise, it became obvious that the framework should embed a dedicated data access framework, that could deal both with asynchronous remote connections and local (server) data computing.

6 For more info, see http://blogs.msdn.com/b/data/archive/2006/12/22/data-access-api-of-the-day-part-iii-disconnected-

programming-in-a-managed-environment.aspx

Page 7: LINQ

Linq & ADO.NET Entities

ADO.NET Entities is built on top of ADO.NET.

The main idea was to develop applications relying on a conceptual model rather than a relationnal DB schema.

The approach of ADO.NET Entities is entirely client-sided, which means that databases don’t need any modification to work with, once an ADO.NET provider exists.

Another main advantage is that an application emits standard SQL, so it’s manageable, verifiable and readable.

7 For more infos, see http://blogs.msdn.com/b/data/archive/2007/01/23/data-access-api-of-the-day-_2800_part-iv_2900_.aspx

Page 8: LINQ

II) What is LINQ ?

Page 9: LINQ

Linq is about data queries

The most important part of Linq is about getting sequences from data sources.

By default, the data sources that can be requested by Linq are :

- Object (Linq to Objects)

- XML fragments or docs (Linq to XML)

- ADO.NET DataSet ( Linq to DataSet)

- Sql Server (Linq to SQL)

- Entity Framework (Linq to Entities)

In theory, every kind of data source can be handled by Linq, with custom Linq providers ( as Linq to Google or Linq to Lucene)

9

Ressources to create your own linq provider:

http://msdn.microsoft.com/en-us/library/vstudio/bb546158.aspx

http://msdn.microsoft.com/en-us/library/system.linq.expressions.expressionvisitor.aspx

Page 10: LINQ

Linq can do more than queries

You can use LINQ to transform set of data :

- Convert your data :

10

Page 11: LINQ

- Transform your data :

11

Page 12: LINQ

III) C# language enhancements

• A) Lambda Expressions

• B) Expression Trees

• C) ‘var’, object and collection initialization and anonymous types

• D) Extensions methods

• E) Partial Methods

• F) Query expression 12

Page 13: LINQ

A) Lambda Expressions : get rid of delegates

Base Code :

13

Page 14: LINQ

A) Lambda Expressions : get rid of delegates

- Old fashionned : named methods The first way that allowed developers to customize the behavior of a core class without having to use inheritance was to expose delegates and use it inside the core method. Here, we are explicitly declaring the delegate filter we want to use :

14

Page 15: LINQ

A) Lambda Expressions : get rid of delegates

- A step further : anonymous methods In framework 2.0, this feature was added in order to reduce amount of code needed :

Indeed, if the delegate is used only once, there is no need to expose it.

But it remains verbose and hard to read.

15

Page 16: LINQ

A) Lambda Expressions : get rid of delegates

- Linq fashion : Lambda

Here, the delegate is subtituted by a Lambda. Lambda expression is a concept coming from functionnal programming, and that provides shorthand for specifying an algorithm.

Lambda expressions are specified as a comma-delimited list of parameters followed by the lambda operator, followed by an expression or statement block (param1, param2, …paramN) => expr

(param1, param2, …paramN) => { statement1; return (lambda_expression_return_type); } 16

Page 18: LINQ

C) ‘var’, object and collection initialization and anonymous types

- Object initialisation :

Short hand to instantiate objects :

Allows to initialize object’s values at instantiation time.

To use this feature, a class should have an accessible constructor.

18

Page 19: LINQ

C) ‘var’, object and collection initialization and anonymous types

- Collections Initialisation

Allows to initialize collection of object values at instantiation time.

To use this feature, a collection class should have an accessible constructor.

19

Page 20: LINQ

D) Extensions methods

- Extensions methods aim to extend behavior of a class, by adding features without modifying the base type.

- An extension method is a static method of a static class that take as first parameter an instance of the extended type prefixed by the « this » keyword.

Call :

Linq is heavily based on extensions, that target in particular IEnumerable and IQueryable interfaces,

20

Page 21: LINQ

E) Partial Methods

Lightweight event-handling mechanism.

gives

Adding a partial method

gives

21

Page 22: LINQ

E) Partial Methods

More efficient than inheritance to allow many potentially unimplemented hooks in code.

This feature is heavily used by Linq to Entities code generator, to make generated entities more usables,

• Partial methods must be defined and implemented only in partial classes.

• Partial methods must specify the partial modifier.

• Partial methods are private but must not specify the private modifier, or a compiler error will result.

• Partial methods must return void.

• Partial methods may be unimplemented.

• Partial methods may be static.

• Partial methods may have arguments.

22

Page 23: LINQ

F) Query expression • Query expression is a paradigm shift that allows to run queries against sets

of data in a near SQL fashion : - first line represent the targeted dataset.

- second line abstracts a WHERE clause.

- third line show the projection clause.

- last line executes the query.

Fluent linq api version :

Some Linq operators are available only in query expression syntax.

Standard and query syntax can be mixed.

In some case, the query expression syntax is more understable than fluent API

23

Page 24: LINQ

IV) Base Concepts

• A) Linq to object

• B) Func<> delegate

• C) Defered vs Undefered operators

• D) Base defered operators

• E) Base undefered operators

• F) Tips and tricks!

24

Page 25: LINQ

A) Linq to Objects

- Linq to Objects is the base functionnality of Linq.

- It targets sequences of objects (collections, lists, arrays) and yields most often IEnumerable<T> (undefered operators) or IQueryable<T> (defered operators)

- Linq to Objects functiunalities are under the System.Linq namespace

25

Page 26: LINQ

B) Func<> delegates

A lot of Linq operators take a Func<> parameter.

For example, check the Where operator :

This type allow to wrap a delegate function, in lambda expression form.

26

Page 27: LINQ

C) Defered vs Undefered operators

Defered operators don’t execute queries until explicitly asked. Futhermore, they return a query, not the result of query :

Gives the following result :

As you can see, each time that maleEmployees is

enumerated, the query is executed.

27

Page 28: LINQ

C) Defered vs Undefered operators

Unefered operators exucute queries directly. Here .ToList() operator is an undefered one

Gives the following result :

maleEmployees will not change when

adding a new entry to employees

28

Page 29: LINQ

D) Base defered operators

Restriction

- Where

Projection

- Select

- SelectMany

Partitioning

- Take

- TakeWhile

- Skip

- SkipWhile

Concatenation

- Concat

Set

- Distinct

- Union

- Intersect

- Except

Conversion

- Cast

- OfType

- AsEnumrable

Element

- DefaultIfEmpty

Generation

- Range

- Repeat

- Empty

29

Sorting

- OrderBy

- OrderByDescending

- ThenBy

- ThenByDescending

- Reverse

Join

- Join

- GroupJoin

Grouping

- GroupBy

Page 30: LINQ

D) Base defered operators

Where() operator

Is used to refine data following the passed predicate

Has two prototypes :

Only the predicate differs. The second one pass the item index to the predicate :

In a general way, several Linq operators offer an additionnal « predicate with index » prototype, check out the System.Linq namespace of System.Core assembly in the Object Explorer!

30

Page 31: LINQ

D) Base defered operators Projection operators

- Select :

This operator aim is to specify and cutomize the output of a query.

will return an IEnumerable<String>.

When used without selector simply return the full object

- SelectMany :

Is used to create one-to-many sequences, It always takes a selector parameter.

31

Page 32: LINQ

D) Base defered operators

Partitioning Operators :

- Take()

- TakeWhile()

- Skip()

- SkipWhile()

Thoses operators allow to split sequences easily, for paging purpose, for example.

32

Page 33: LINQ

D) Base defered operators

Concatenation:

- Concat() :

It allows to simply concat sequences of the same type:

33

Page 34: LINQ

D) Base defered operators Ordering:

Those operators allow to simply order sequences.

- OrderBy() and OrderByDescending() :

Return IOrderedEnumerable<T> and cannot take IOrderedEnumerable<T> parameter

- ThenBy() end ThenByDescending() :

Return IOrderedEnumerable<T> and must take IOrderedEnumerable<T> parameter

Each one has an additionnal prototype taking an IComparer, if a custom ordering is needed

- Reverse() :

Simply output a reversed sequence. 34

Page 35: LINQ

D) Base defered operators Joining:

- Join()

It performs an equi-join between sequences :

- GroupJoin()

Perform a join between sequences, and enumerates the inner sequence

35

Page 36: LINQ

D) Base defered operators Set operators

Thoses operators are used to perform mathematical set-type operations on sequences

- Distinct() : Removes duplicates elements in sequence

- Union() : Yields element of input sequences, and then yields element of second sequence that are not in the first one

- Intersect() : Yields elements that are in both sequences

- Except() : Yields the elements of the input sequence that are not in the 2nd one

36

Page 37: LINQ

D) Base defered operators Conversion operators

- Cast()

- OfType() :

Cast() and OfType target non-generic Ienumerable interface, in order to open Linq functionnalities to legacy collections.

Cast() will throw an exception if it cannot cast an element of the input sequence, while OfType will simply ignore the uncastable elements

- AsEnumrable() :

Cast a sequence into a normal IEnumerable<T> in order to use standard Linq operators. For example, Select() in Linq To SQL returns IQueryable<T> that don’t implement the Reverse() operator,

37

Page 38: LINQ

D) Base defered operators Element operator

- DefaultIfEmpty()

This operator will generate a default element if the output sequence is empty.

Basically, it avoids the System.InvalidOperationException: Sequence contains no

elements

38

Page 39: LINQ

D) Base defered operators Generation operators

- Range()

Static method (not a extension) that generates a sequence of integers,

- Repeat()

Its generates a sequence of the same specified element.

- Empty()

Static method (not a extension) that generates an empty sequence of the requested type

39

Page 40: LINQ

E) Base undefered operators

Conversion

- ToArray

- ToList

- ToDictionary

- ToLookup

Element

- First

- FirstOrDefault

- Last

- LastOrDefault

- Single

- SingleOrDefault

- ElementAt

- ElementAtOrDefault

Quantifiers

- Any

- All

- Contains

Equality

- SequenceEqual

Aggregate

- Count

- LongCount

- Sum

- Min

- Max

- Average

- Aggregate

40

Page 41: LINQ

E) Base undefered operators

Conversion operators

- ToArray() :

Take an IEnumrable<T> and return an array T[].

- ToList() :

Take an IEnumrable<T> and return a List<T>.

- ToDictionary()

- ToLookup() :

Have the same prototypes, and rely on Func<> to select Keys and Elements :

41

Page 42: LINQ

E) Base undefered operators

Element Operators

- First

- FirstOrDefault

- Last

- LastOrDefault

- Single

- SingleOrDefault

- ElementAt

- ElementAtOrDefault

Self speaking methods. Just keep in mind that the « OrDefault » version will generate a default element if no element is found whereas classic version will throw an exception.

42

Page 43: LINQ

E) Base undefered operators

Quantifiers Operators

- Any()

Returns true if at least one element of the sequnce match the predicate

- All()

Returns true if all elements of the sequnce match the predicate

- Contains()

Returns true if the sequence contains the parameter item.

Equality operator

- SequenceEqual()

It determines whether two input sequences are equal.

43

Page 44: LINQ

E) Base undefered operators

Aggregate operators

- Count()

- LongCount()

- Sum()

- Min()

- Max()

- Average()

Thoses operators usage is obvious, just keep in mind that they can take a predicate parameter, saving a call to Where()

44

Page 45: LINQ

E) Base undefered operators

Aggregate operators

- Aggregate()

It’s a tricky but powerful one! The Aggregate operator performs a user-specified function on each element of an input sequence, passing in the function’s return value from the previous element and returning the return value of the last element.

- No seed prototype :

Fast, isn’t it?

- With seed :

45

Page 46: LINQ

F) Tips and tricks!

- When you cannot know the returned type of a query, do not hesitate to use the var keyword!

- You can use Cast or OfType operators when you have to query a legacy collection!

- In query expression, you can use the « Let » clause, that allows to query subsets in the current query :

46

Page 47: LINQ

F) Tips and tricks!

- Get Linqpad!

It’s a simple ( and free! ) tool that allows you to test your queries, your code snippets. You can even query the BDD :

http://www.linqpad.net/

47

Page 48: LINQ

V) Advanced concepts

• A) Linq To DatatSet

• B) Linq To XML

• C) Linq To SQL

48

Page 49: LINQ

V) Advanced concepts

• A) Linq To DatatSet

Linq to Dataset relies on the System.Data namespace part embedded in System.Data.DataSetExtensions Assembly.

Linq to DataSet is a very useful feature when dealing with legacy code that relies on standard ADO.NET DataSet, DataTablr, DataRow and DataColumn.

The first thing to do is to call the .AsEnumerable() extension on your DataSet or DataTable, and this one become Linq-ready!

Linq To DataSet could be view as an extension of Linq To Object, because its matter is to adapt specific objects to make its queryable through Linq.

49

Page 50: LINQ

V) Advanced concepts

• A) Linq To DatatSet

DataRow Operators :

Set operators

- Distinct()

- Except()

- Intersect()

- Union()

- SequenceEqual()

Fields Operators

- Field<T>() : obtain the value of a column from a DataRow object and handles the casting of DBNull

- SetField<T> () : allow to change a value of a property (column) in a DataRow

50

Page 51: LINQ

V) Advanced concepts

• A) Linq To DatatSet

DataTable Operators :

- AsEnumerable() :

Yield an IEnumerable<DataRow>

- CopyToDataTable<DataRow>() :

Copy a set of DataRow in the targeted DataTable.

51

Page 52: LINQ

V) Advanced concepts

• B) Linq To XML

Linq To Xml relies on the System.Xml.Linq

Obviously, Linq To XML first intention is to query XML with Linq. But Microsoft takes the opportunity to add a new (and waaay easier) XML API.

Indeed, when it’s about creating XML, previous API ( called W3C DOM XML API ) was so cumbersome that most developers prefer to build XML-like strings.

Linq To XML allows to divide the amount of code needed by 3 when creating XML.

52

Page 53: LINQ

V) Advanced concepts

• B) Linq To XML

Here is the X-Linq Object Model :

53

Page 54: LINQ

V) Advanced concepts

• B) Linq To XML

XML Creation :

With X-Linq, creating Xml is very easy, thank to the fluent API.

When a full (and valid) document is needed, simply put an Xdocument as root element :

54

Page 55: LINQ

V) Advanced concepts

• B) Linq To XML

XML Output:

X-Linq offers simple Save mechanism:

XDocument.Save and XElement.Save

The second one will automatically add the xml header.

XML Input :

X-Linq allow you to work with external xml document and fragement

- Load() : Allow to convert raw XML fragements into XDoc or XElt

- Parse() : Allow to convert non formarted xml strings into XDoc or Xelt

55

Page 56: LINQ

V) Advanced concepts

• B) Linq To XML

XML Traversal Methods:

- forward :

- XNode.NodesAfterSelf()

- XNode.ElementsAfterSelf()

- backward :

- XNode.NodesBeforeSelf()

- XNode.ElementsBeforeSelf()

- down :

- XContainer.Nodes()

- XContainer.Elements()

- XContainer.Element()

- XContainer.Descendants() recursive

- XElement.DescendantsAndSelf() recursive

- up :

- XNode.Ancestors() recursive

- XElement.AncestorsAndSelf() recursive

56

Page 57: LINQ

V) Advanced concepts

• B) Linq To XML

XML Traversal Properties:

- forward : XNode.NextNode

- backward : XNode.PreviousNode

- up : XObject.Document

XObject.Parent

57

Page 58: LINQ

V) Advanced concepts

• B) Linq To XML

X-LINQ operators:

- Ancestors()

- AncestorAndSelf()

- Attributes()

- DescendantNodes()

- DescendantNodesAndSelf()

- Descendants()

- Elements()

- InDocumentOrder()

- Remove()

58

Page 59: LINQ

V) Advanced concepts • C) Linq To SQL

Linq to SQL is an API to work with MS SqlServer DBs. Its aim it to make the bridge between our classes and the way their data are stored, and to make this bridge easier to craft and maintain. It’s an entry-level ORM.

To enable Linq to SQL, there are some prerequisites :

- Generating the Entity classes, with SQLMetal utility, for example.

=> create classes that reflect the DB model

- Generating the XML mapping file

=> create a file that will allow to map objects into sql entries and sql queries results into objects, automatically

59

Generating the Entity classes :

in vs command prompt

sqlmetal /namespace:nwind /code:Northwind.cs /pluralize /functions /sprocs /views <path to Northwind MDF file>

Generating the xml mapping file :

in vs command prompt

sqlmetal /map:northwindmap.xml "C:\Northwind.mdf" /pluralize /functions /sprocs /views /namespace:nwind /code:Northwind.cs

Page 60: LINQ

V) Advanced concepts

• C) Linq To SQL

DataContext

One key concept of LINQ to SQL is the DataContext class, that establishes database connection, holds common methods to interact with databases, holds the references to the entities.

When using Linq To SQL, the first thing to do is to instantiate your DataContext

60

Page 61: LINQ

V) Advanced concepts

• C) Linq To SQL

Standard Database operations

Once your DataContext is instantiated, you can start to query it with standard Linq queries. There is two point to keep in mind :

- Linq to SQL operators return IQueryable<T>

- Queries are translated into SQL, meaning that some advanced restriction clause may throw exceptions at runtime, like custom user type comparison.

In the following, an example will be given in the Northwind DataContext.

61

Page 62: LINQ

V) Advanced concepts

• C) Linq To SQL

Standard Database operations

- Select :

- Join :

62

Page 63: LINQ

V) Advanced concepts

• C) Linq To SQL

Standard Database operations

- Insert :

63

Page 64: LINQ

V) Advanced concepts

• C) Linq To SQL

Standard Database operations

- Delete :

64

Page 65: LINQ

V) Advanced concepts

• C) Linq To SQL

Standard Database operations

- Update:

65

Page 66: LINQ

VI) Examples

• A) Build a querystring formated based on a dictionary

• B) Convert a enum to a list of its values

• C) Extract data from legacy dataset

66

Page 67: LINQ

VI) Examples

A) Build a querystring formated based on a dictionary

67

Page 68: LINQ

VI) Examples

B) Convert a enum to a list of its values

68

Page 69: LINQ

VI) Examples

C) Extract data from legacy dataset

69

Page 70: LINQ

VII) Going Deeper !

• A) MoreLinq library

• B) DinamycLinq Library

• C) Linq To whatever : build you own IQueryable

- Steps (msdn)

- Linq to Google

- Linq to Lucene

70

Page 71: LINQ

VII) Going Deeper • MoreLinq Library - Available by Nuget : Add usefull operator

71

Operator Summary

AssertCount Asserts that a source sequence contains a given count of elements.

Batch Batches the source sequence into sized buckets.

Concat Returns a sequence consisting of the head element and the given tail elements. This operator uses deferred execution and streams its results.

Consume Completely consumes the given sequence. This method uses immediate execution, and doesn't store any data during execution.

DistinctBy Returns all distinct elements of the given source, where "distinctness" is determined via a projection and the default eqaulity comparer for the projected type.

EquiZip Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences.

ForEach Immediately executes the given action on each element in the source sequence.

Generate Returns a sequence of values consecutively generated by a generator function.

GenerateByIndex Returns a sequence of values based on indexes.

MaxBy Returns the maximal element of the given sequence, based on the given projection.

MinBy Returns the minimal element of the given sequence, based on the given projection.

Pad Pads a sequence with default values if it is narrower (shorter in length) than a given width.

Pipe Executes the given action on each element in the source sequence and yields it.

Prepend Prepends a single value to a sequence.

PreScan Performs a pre-scan (exclusive prefix sum) on a sequence of elements.

Scan Peforms a scan (inclusive prefix sum) on a sequence of elements.

SingleOrFallback Returns the single element in the given sequence, or the result of executing a fallback delegate if the sequence is empty.

TakeEvery Returns every N-th element of a source sequence.

ToDelimitedString Creates a delimited string from a sequence of values. The delimiter used depends on the current culture of the executing thread.

Trace Traces the elements of a source sequence for diagnostics.

Zip Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences.

ZipLongest Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences.

Page 72: LINQ

VII) Going Deeper

• Dynamic Linq Library

Library provided by Microsoft.

Allow to build dynamic linq queries, by making restriction and ordering operator accepting string instead of lambda

Classic query

Dynamic Liq Query

Exemple from ScottGu’s blog

72 http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

http://weblogs.asp.net/davidfowler/archive/2010/08/19/dynamic-linq-part-2-evolution.aspx

Page 73: LINQ

VII) Going Deeper

Linq To whatever : build you own IQueryable!

Linq is not limited to the Microsoft’s tools. You can build a Linq API to nearly every datasource, since you create Iqueryable LINQ provider.

Numerous projects take advantage of this opportunity.

The most well known are

- LINQ to Google (GLinq) : you can query Google’s datasources easily

- Linq to Lucene : Lucene is a strong, fast, lightweight and open source text search engine. Linq to Lucene allow you to simplify access to data indexed by Lucene

73

Microsoft guide http://msdn.microsoft.com/en-us/library/vstudio/bb546158.aspx

GLinq : http://glinq.codeplex.com/

Linq to lucene : http://linqtolucene.codeplex.com/

Page 74: LINQ

VIII) Questions?

74

Page 75: LINQ

Find out more

• On https://techblog.betclicgroup.com/

Page 76: LINQ

About Betclic • Betclic Everest Group, one of the world leaders in online gaming, has a unique portfolio

comprising various complementary international brands: Betclic, Everest Gaming, bet-at-home.com, Expekt…

• Active in 100 countries with more than 12 million customers worldwide, the Group is committed to promoting secure and responsible gaming and is a member of several international professional associations including the EGBA (European Gaming and Betting Association) and the ESSA (European Sports Security Association).

• Through our brands, Betclic Everest Group places expertise, technological know-how and security at the heart of our strategy to deliver an on-line gaming offer attuned to the passion of our players.