1. Trang chủ
  2. » Tất cả

Chapter 3 - LINQ Query Operators

27 455 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 27
Dung lượng 212,1 KB

Nội dung

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelLINQ via C# 3.0Chapter 3LINQ Query Operators © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelQuery 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 IsraelWhat 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 IsraelWhy Query Operators?•Query operators facilitate declarative programming instead of imperative programmingList<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);BecomesThe HowThe What © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelDeveloping 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 IsraelWriting “Where” By Handstatic 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 IsraelCompiler 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 IsraelWhere’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 IsraelBehind The Magicprivate 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 IsraelRevisiting “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> [...]... – We can throw the query away without executing it © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel Review So Far • Query operators as declarative programming • Our hand-rolled query operators • Deferred execution © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel Standard LINQ Query OperatorsLINQ (in System.Core.dll)... Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel Standard LINQ Query OperatorsLINQ query operators are extension methods in the static Enumerable class • Can be combined with our own! – As long as there’s no name collision • Note: So far we have discussed LINQ to Objects query operators – Other LINQ provides do not work with IEnumerable © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch... Language Query Operators • Language query operators are language keywords that are mapped to query operators – Not all query operators are accessible via keywords • C# integrated query operators: – – – – – Projection: select Filtering: where Grouping: group by Ordering: orderby Miscellaneous: join, let © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel Language Query. .. Brak 51202 Israel Syntactic Sugar • Language query operators translate directly to LINQ query operatorsQuery operators can be mixed: int cnt = (from i in Enumerable.Range(0, 100) where i % 2 == 0 select i).Count(i => i > 25); © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel Advanced Example: Joins • Most SQL-like joins are unnecessary because objects are... OrderCount = orders.Count }; © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel Chapter Summary • Why query operators? • Developing custom query operators • Language integrated query operators © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel ... Empty source } © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel Our Operators In Action • This gives us some room for experimentation: var numbers = Range(0, 100); var query = numbers.Where(i => i % 2 == 0); Console.WriteLine("{0} numbers, first: {1}", query. Count(), query. First()); foreach (int i in query) Console.WriteLine(i); Employee[] employees = ; var sortedEmpIds... Language Query Operators var q2 = from i in Enumerable.Range(0, 100) select new { Id=i, Name=i.ToString() }; var q3 = from e in q2 where e.Name.StartsWith("1") orderby e.Id descending group e by e.Name.Substring(e.Name.Length-1) into g select g; var here is IEnumerable foreach (var group in q3) var here is IGrouping © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch... Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel Advanced Example: Let • let is a helper keyword for complex queries: var query = from c in customers where c.Joined.AddYears(1) < DateTime.Now let orders = c.Orders.ToList() select new { Name = c.Name, OrderQuantity = orders.Sum(o=>o.Amount) OrderCount = orders.Count }; © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St... Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel Examples Of LINQ Operators var q = Enumerable.Range(0, 100) Select(i => new { Id=i, Name=i.ToString() }) Where(e => e.Name.StartsWith("1")) OrderByDescending(e => e.Id) GroupBy(e => e.Name.Substring(e.Name.Length-1)) Select(grp => grp); foreach (var group in q) { Console.WriteLine("Key: " + group.Key); Console.WriteLine("Elements:... Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel Deferred Execution • Statements such as var numbers = Range(0, 100); var evens = numbers.Where(i => i%2==0); provide deferred execution semantics • The enumeration is not executed yet! – foreach forces execution – Our Order operator forces execution – Our Count operator forces execution © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch . 1 4-1 8 Baruch Hirsch St. Bnei Brak 51202 IsraelLINQ via C# 3. 0Chapter 3 – LINQ Query Operators © Copyright SELA Software & Education Labs Ltd. 1 4-1 8. Ltd. 1 4-1 8 Baruch Hirsch St. Bnei Brak 51202 IsraelDeveloping Query Operators Most query operators are very straightforward•Let’s develop some query operators

Ngày đăng: 12/01/2013, 16:18

TỪ KHÓA LIÊN QUAN