LINQ and LINQPad

11
LINQ Andreas Gullberg Larsen

Transcript of LINQ and LINQPad

Page 1: LINQ and LINQPad

LINQAndreas Gullberg Larsen

Page 2: LINQ and LINQPad

What is LINQ?Language Integrated Query - part of Microsoft .NET Framework

Formulate queries directly in C# and VB instead of SQL

LINQ-to-objects (in-memory data)

LINQ-to-entities (ORM <-> SQL)

Page 3: LINQ and LINQPad

Why LINQ?No need to learn a new query syntax, use your C# programming chops.

Single, consistent query language for working with data - in memory or in databases.

Widely used, adapters for most databases you can think of:MySQL, MariaDB, MSSQL, PostgreSQL, MongoDB, CouchDB, ...

Page 4: LINQ and LINQPad

LINQ - Two tales of syntaxQuery syntax - “SQL-like”

Method syntax - “C#-like”

Page 5: LINQ and LINQPad

LINQ - OperationsSelect (map)

SelectMany (flatten)

Page 6: LINQ and LINQPad

LINQ - OperationsWhere (filter)

Aggregrate (reduce)

Page 7: LINQ and LINQPad

LINQ - OperationsOrdering

JoinGroupByAggregates - Min, Max, Sum, Count, AverageSet - Union, Distinct, Intersect, ExceptElement - First, FirstOrDefault, Last, LastOrDefaultQualifiers - Any, All, ContainsPartitioning - Take, SkipConversions - ToList, ToDictionary, ToArray, Cast<T>, OfType<T>

Page 8: LINQ and LINQPad

Extending LINQ-to-objectsIt is all about IEnumerable

Nugets: MoreLINQ

Writing your own Prepend()

This won’t work against databases!

Page 9: LINQ and LINQPad

LINQ-to-entities (databases)Requires an ORM, Entity Framework ships with .NET for SQL

Has adapters to all the popular databases, MSSQL, MySQL, MariaDB, PostgreSQL…

LINQ is translated to SQL, so this doesn’t work

But this does:

Page 10: LINQ and LINQPad

LINQ-to-entities (databases)LINQ is lazy, no query is run until you call .ToList().

Page 11: LINQ and LINQPad

LINQPadAwesome C# and LINQ scratchpad.

Test out C#, F#, VisualBasic code-snippets

Test out queries with LINQ, SQL, ESQL against databases

Navigate the database from any result set, follow relations and dig deep into the data

Intellisense and supports nugets

Showtime!