COMPSCI 335 LINQ (part 1 of 2) radu/2008 LINQ – BIRD S EYE ...

COMPSCI 335

LINQ (part 1 of 2)

LINQ ? BIRD'S EYE VIEW LINQ = Language Integrated Native Queries

radu/2008

.NET extensions that allows uniform integration of data from various sources, such as (not exhaustive): o Databases: LINQ to SQL (formerly DLINQ), LINQ to , LINQ to Entities o XML Documents: LINQ to XML with XPATH support (formerly XLINQ) o Objects in memory: LINQ (to Objects)

Also used to build parallel programs that can efficiently use modern multi-core systems o Parallel LINQ (PLINQ)

Ingredients: o Query expressions o Extension Methods o Lambda Expressions o Iterators o Generics o Type Inferrence o Anonymous Types

from ... in Collection where ... orderby ... select

Collection . Where(...) . OrderBy(...) . Select(...)

x => f(x)

The query expression syntax:

foreach, yield

o aka comprehension syntax

o linguistic extension inspired by SQL

var new { ... }

o automatically translated into the extension method syntax

1 of 22

COMPSCI 335

LINQ (part 1 of 2)

radu/2008

LINQ ? MAIN READINGS FOR THIS HANDOUT

Texts and examples based on MSDN Library for VS 2008

o Visual Studio o Visual C# C# Programming Guide ? LINQ Query Expressions

A great book: C# 3.0 in a Nutshell Joseph Albahari, Ben Albahari O'Reilly

o Query Expression Basics

o They have also created the LINQPad utility

o Visual Studio o .NET Framework Programming in Visual Studio Language-Integrated Query (LINQ) ? Introduction to LINQ o LINQ General Programming Guide

o Nice chart that describes query expression syntax

syntax.html

Standard Query Operators Overview

? ...

? ...

2 of 22

COMPSCI 335

LINQ (part 1 of 2)

LINQ ? HOW DOES IT WORK?

radu/2008

LINQ Objects (sequences)

yield ...

Query Expression

from x in C where ... select ...

Lang Rules

Method syntax C.Where(..).Select(..)

DLINQ SQL, ... SELECT ... FROM C

WHERE ...

XLINQXML/XPATH

Custom ...

The final destination depends on the type of C. o If C is IEnumerable, then .NET uses the extension methods from System.Linq.Enumerable,

which operates in memory, on sequences of objects ? typically "lazy" deferred execution o If C is IQueryable, then .NET uses the extension methods from the derived

System.Linq.Queryable, which first builds an expresion tree in memory, then sends it to the "provider" for execution ? usually the DBMS! o ...

3 of 22

COMPSCI 335

LINQ (part 1 of 2)

STANDARD QUERY OPERATORS ON OBJECT SEQUENCES

radu/2008

Query extension syntax from ... in ... let ... = ... where ... join ... in ... on ... equals ... join ... in ... on ... equals ... into orderby ... , ... orderby ... ascending orderby ... descending select ... group ... by ... into ...

Additional reading: CSharp Language Specification.DOC esp., sections 14, 15

Method syntax

o basic methods:

Where (...)

Select (...)

SelectMany (...)

Join (...)

inner

o System.Linq.Enumerable implements such methods for generic IEnumerable sequences

GroupJoin (...) outer left OrderBy (...) OrderByDescending (...) GroupBy (...) GroupBy (...) o after ordering:

ThenBy (...) ThenByDescending (...) o after grouping:

o It also implements other useful auxiliary functions, that don't appear in the query syntax, e.g.:

Distinct(), Count(), OfType(), Cast(), AsEnumerable(), Aggregate(), Sum(), ...

o A complete summary is at the end of the handout

Key { get; }

4 of 22

COMPSCI 335

LINQ (part 1 of 2)

Memento

As we will see, System.Linq.Enumerable implements most of the query methods, such as Where, Select, OrderBy, ...

o as extension methods o typically used in the instance notation, and cascaded

o that return IEnumerable sequences, built via iterators, therefore o these methods return enumerators with "hidden loops" the user doesn't have to write many loops (if at all) ? this is typical of functional programming o they use the lazy evaluation pattern the resulting sequence is only build when really needed, e.g., ? when enumerated by a foreach loop ? when counted ? when actually stored (buffered) in an array or List

radu/2008

5 of 22

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download