COMPSCI 335 LINQ (part 1 of 2) radu/2008 LINQ – BIRD S EYE ...
[Pages:22]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
COMPSCI 335
LINQ (part 1 of 2)
radu/2008
About our samples
o Most are presented in the LINQPad format
o We often transform our IEnumerable collections into IQueryable collections and invoke .Dump() on query expressions
o Why? This is not required, but it allows us to visualize the translation from the query syntax to the method syntax
var highScoresQuery = from score in scores.AsQueryable() where score > 80 orderby score descending select score;
highScoresQuery.Dump();
System.Int32[] .Where (score => (score > 80)) .OrderByDescending (score => score)
Above int[] stands for our highScoresQuery array, thus the actual method calls is
highScoresQuery .Where (score => (score > 80)) .OrderByDescending (score => score)
6 of 22
COMPSCI 335
LINQ (part 1 of 2)
Sorting
OrderBy, OrderByDescending, ThenBy, ThenByDescending, Reverse
radu/2008
string[] sequence = { "the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" };
// unity sort var s1 = sequence.OrderBy(s => s);
o OrderBy ... use an internal OrderedSequence class
o to allow further stable sorting via ThenBy ...
o See Linq.Enumerable
// unity sort descending var s2 = sequence.OrderByDescending(s => s);
// sort by length var s3 = sequence.OrderBy(s => s.Length);
// sort by length descending var s4 = sequence.OrderByDescending(s => s.Length);
// sort by length then by value var s5 = sequence.OrderBy(s => s.Length)
.ThenBy(s => s);
Equivalent query expressions
var s1 = from s in sequence orderby s select s;
and, for the other cases
orderby s descending ... orderby s.Length ... orderby s.Length descending ... orderby s.Length, s ...
var s7 = sequence.Reverse();
7 of 22
COMPSCI 335
LINQ (part 1 of 2)
Set Operations Distinct, Except, Intersect, Union
radu/2008
string[] sequence = { "the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" };
var distinct = sequence.Distinct();
Implementation in System.Linq.Enumeration public static IEnumerable Distinct( this IEnumerable source ) {
Dictionary dict = new Dictionary();
foreach (T element in source) { if (!dict.ContainsKey(element)) { dict.Add(element, null); yield return element; }
} }
8 of 22
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- bird s eye maple wood
- 1 of 2 page numbering
- a bird s eye view meaning
- 1 or 2 912 912 1 0 0 0 1 example of present tense
- 1 or 2 577 577 1 0 0 0 1 example of epic story
- 1 or 2 606 606 1 0 0 0 1 example of present tense
- 1 or 2 869 869 1 0 0 0 1 example of epic story
- 1 or 2 169 169 1 0 0 0 1 example of epic story
- 1 or 2 142 142 1 0 0 0 1 example of present tense
- 1 or 2 742 742 1 0 0 0 1 or s6nx54on example of present tense
- 1 or 2 635 635 1 0 0 0 1 or wbyigdap example of epic story
- 1 or 2 478 478 1 0 0 0 1 example of present tense