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

Chapter 4 - LINQ to Objects

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 (239.79 KB, 15 trang )

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

Using language integrated query
operators with objects

Customizing query operators for particular
objects

Examples, examples, examples
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
LINQ to Objects

LINQ to Objects relies on the Enumerable
class, which contains query operators as
extension methods

Any IEnumerable<T> can be queried

Additional types can be queried if they
implement the query pattern
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Query Pattern

If the type is not IEnumerable, there are
two ways to make it queryable:

Add instance methods for query operators: Select<T>,


Where, OrderBy, etc.

Add extension methods in a separate class for query
operators: Select<T>, Where, OrderBy, etc.
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Query Pattern: Select
class PayrollSystem {
public IEnumerable<T> Select<T>(
Func<Employee,T> selector) {
return employees.Select(selector);
}
internal List<Employee> employees = ...;
//More implementation omitted
}
PayrollSystem payroll = ...;
var names = from employee in payroll
select employee.Name;
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Query Pattern: Select Extension

Alternatively, an extension method can be
used:
class PayrollSystem {
internal List<Employee> employees = ...;
//More implementation omitted
}
static class PayrollExtensions {
public static IEnumerable<T> Select<T>(
this PayrollSystem payroll,
Func<Employee,T> selector) {

return payroll.employees.Select(selector);
}
}
PayrollSystem payroll = ...;
var names = from employee in payroll
select employee.Name;

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×