Tải bản đầy đủ (.pptx) (27 trang)

Chapter 3 - LINQ Query Operators

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (364.81 KB, 27 trang )

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
LINQ via C# 3.0
Chapter 3 – LINQ Query Operators
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Query Operators

Why query operators?

Developing query operators for
IEnumerable<T>

Language integration
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
What Are Query Operators?

Query operators are typically extension
methods that operate on a sequence of
elements

Some query operators return a sequence
of elements, some return a single value
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Why Query Operators?

Query operators facilitate declarative
programming instead of imperative
programming
List<int> sortedPrimes =
new List<int>();
foreach (int i in numbers) {
if (IsPrime(i)) {


sortedPrimes.Add(i);
}
}
sortedPrimes.Sort();
sortedPrimes.ForEach(
Console.WriteLine);
numbers
.Where(i => IsPrime(i))
.OrderBy(i => i)
.ToList()
.ForEach(
Console.WriteLine);
Becomes
The How
The What
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Developing Query Operators

Most query operators are very
straightforward

Let’s develop some query operators that
operate on IEnumerable<T>

Where (filtering a sequence)

Order (ordering a sequence)

Select (projecting a sequence to another sequence)


Count (aggregating a sequence)

First (first element)
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Writing “Where” By Hand
static class EnumerableExtensions {
public static IEnumerable<T> Where<T>(
this IEnumerable<T> source,
Predicate<T> filter) {
return WhereEnumerable<T>(source, filter);
}
}
class WhereEnumerable<T> : IEnumerable<T> {
//...
public IEnumerator<T> GetEnumerator() {
return new WhereEnumerator<T>(...);
}
}
class WhereEnumerator<T> : IEnumerator<T> {
//...
}
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Compiler Magic To The Rescue

C# 2.0 includes compiler support for
generating IEnumerable implementations

Just yield them!
public static IEnumerable<int> Range(
int min, int max) {

for (; min < max; ++min)
yield return min;
}
foreach (int num in Range(0, 100))
Console.WriteLine(num);
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Where’s The Magic?

The compiler generates code (edited):
private sealed class <Range>d__0 :
IEnumerable<int>, IEnumerable, IEnumerator<int>,
IEnumerator, IDisposable {
private int <>1__state;
private int <>2__current;
public int <>3__max;
public int <>3__min;
public int max;
public int min;
private bool MoveNext();
IEnumerator<int> IEnumerable<int>.GetEnumerator();
int IEnumerator<int>.Current { get; }
//More code snipped
}
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Behind The Magic
private bool MoveNext() {
switch (this.<>1__state) {
case 0:
this.<>1__state = -1;
while (this.min < this.max) {

this.<>2__current = this.min;
this.<>1__state = 1;
return true;
Label_003E:
this.<>1__state = -1;
this.min++;
}
break;
case 1:
goto Label_003E;
}
return false;
}
State Machine
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Revisiting “Where”
static class EnumerableExtensions {
public static IEnumerable<T> Where<T>(
this IEnumerable<T> source,
Predicate<T> filter)
{
foreach (T elem in source)
if (filter(elem))
yield return elem;
}
}
var numbers = Range(0, 100);
var evens = numbers.Where(i => i%2==0);
var here is
IEnumerable<int>

×