1. Trang chủ
  2. » Công Nghệ Thông Tin

Chapter 15 - Introduce to LINQ pot

45 297 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 45
Dung lượng 1,29 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

15.1 Understanding the Role of LINQ 15.2 A First Look at LINQ Query Expressions 15.3 LINQ and Generic Collections 15.4 LINQ and Nongeneric Collections 15.5 The Internal Representation of

Trang 2

“LINQ allows you to build strongly typed query expressions, which can be applied to a number of LINQ targets to manipulate “data” in the broadest sense of the word Here, you will learn about LINQ to Objects, which allows you to apply LINQ expressions to containers of data (arrays, collections, custom types) This information will serve you well when we examine how to apply LINQ expressions to relational databases (via LINQ to ADO) and XML documents.”

Trang 3

15.1 Understanding the Role of LINQ

15.2 A First Look at LINQ Query Expressions

15.3 LINQ and Generic Collections

15.4 LINQ and Nongeneric Collections

15.5 The Internal Representation of LINQ Query Operators 15.6 Investigating the C# LINQ Query Operators

Trang 4

15.1 The Role of LINQ

 The LINQ API is an attempt to provide a consistent,

symmetrical manner in which programmers can obtain and manipulate “data” in the broad sense of the term

Trang 5

The Role of LINQ

Trang 6

15.1 Understanding the Role of LINQ

15.2 A First Look at LINQ Query Expressions

15.3 LINQ and Generic Collections

15.4 LINQ and Nongeneric Collections

15.5 The Internal Representation of LINQ Query Operators 15.6 Investigating the C# LINQ Query Operators

Trang 7

15.2 A First Look at LINQ Query

Expressions

 LINQ and Implicitly Typed Local Variables

Trang 8

LINQ Query Expressions

and can’t subtype?

IEnumerable<T>, without deriving a new interface?

type, but the syntax makes them look that way

Trang 9

LINQ Query Expressions

Trang 10

LINQ Query Expressions

T

parameter tells CLR the method is an Extension

the object

Trang 11

Replacing LINQ Extension Methods

members

extension method, the type’s member will be called, and there is

no ambiguity

implementations of the Standard Query Operators

Trang 13

 Lambda expressions

Lambda Expression

Trang 14

Lambda Expression

Trang 15

The Role of Differed Execution

 LINQ query expressions are not actually evaluated until you iterate over their contents

 Apply the same LINQ query multiple times to the same container and obtain the latest and greatest results

Trang 16

The Role of Immediate Execution

 To evaluate a LINQ expression from outside the confines

of foreach logic, you are able to call any number of

extension methods defined by the Enumerable type

 Extension methods:ToArray<T>,

ToDictionary<TSource,TKey>(), and ToList<T>()

 Capture a LINQ query result set in a strongly typed

container

Trang 17

15.1 Understanding the Role of LINQ

15.2 A First Look at LINQ Query Expressions

15.3 LINQ and Generic Collections

15.4 LINQ and Nongeneric Collections

15.5 The Internal Representation of LINQ Query Operators 15.6 Investigating the C# LINQ Query Operators

Trang 18

15.3 LINQ and Generic Collections

 LINQ query expressions can also manipulate data within members of the System.Collections.Generic namespace, such as the List<T> type

Trang 20

Applying a LINQ Expression

 grabbing items from the List<T> where the Speed

property is greater than 55

Trang 21

15.1 Understanding the Role of LINQ

15.2 A First Look at LINQ Query Expressions

15.3 LINQ and Generic Collections

15.4 LINQ and Nongeneric Collections

15.5 The Internal Representation of LINQ Query Operators 15.6 Investigating the C# LINQ Query Operators

Trang 22

15.4 LINQ and Nongeneric Collections

 It is possible to iterate over data contained within

nongeneric collections using the generic

Enumerable.OfType<T>() method

Trang 23

Nongeneric Collections

 Use the previously defined Car type and import the

System.Collections namespace

Trang 24

15.1 Understanding the Role of LINQ

15.2 A First Look at LINQ Query Expressions

15.3 LINQ and Generic Collections

15.4 LINQ and Nongeneric Collections

15.5 The Internal Representation of LINQ Query Operators 15.6 Investigating the C# LINQ Query Operators

Trang 25

15.5 The Internal Representation of LINQ

Query Operators

 Introduced to the process of building query expressions using various C# query operators (such as from, in,

where, orderby, and select)

 C# compiler actually translates these tokens into calls on various methods of the System.Linq.Enumerable type

 Many methods require a generic delegate of type

Func<>

Trang 26

Query Expressions

Trang 27

Query Expressions

syntax

Trang 28

Building Query Expressions with Query Operators

 Building LINQ expressions using various query operators (from, in, where, orderby, etc.) is the most common and most straightforward approach

Trang 29

Building Query Expressions Using the Enumerable Type and Lambdas

Enumerable type directly is much more verbose than making use of the C# query operators

Trang 30

Building Query Expressions Using the

Enumerable Type and Anonymous Methods

Trang 31

Building Query Expressions Using the Enumerable Type and Raw Delegates

Trang 32

 Many methods of Enumerable require delegates (Func<> in particular) as parameters

 Under C# 2008, any method requiring a delegate parameter can instead be passed a lambda expression

 Lambda expressions are simply anonymous methods in

disguise (which greatly improve readability)

Trang 33

15.1 Understanding the Role of LINQ

15.2 A First Look at LINQ Query Expressions

15.3 LINQ and Generic Collections

15.4 LINQ and Nongeneric Collections

15.5 The Internal Representation of LINQ Query Operators 15.6 Investigating the C# LINQ Query Operators

Trang 34

15.6 Investigating the C# LINQ Query

Operators

from, in Used to define the backbone for any LINQ expression, which allows you to extract a subset of data from a

fitting container.

where Used to define a restriction for which items to extract from a containerselect Used to select a sequence from the container

join, on, equals, into Performs joins based on specified key Remember, these “joins” do not need to have anything to do with

data in a relational database.

Trang 35

Building a New Test Project

FunWithLinqExpressions

 Next, define a trivial Car type

 Populate an array with the following Car objects within your Main() method

Trang 36

Example 15.1 Investigating the C# LINQ Query Operators

Trang 37

Basic Selection Syntax

 Ordering of the operators is critical

 var result = from item in container select item;

Trang 38

Obtaining Subsets of Data

 To obtain a specific subset from a container, use the

where operator

var result = from item in container where Boolean

expression select item;

Trang 39

Projecting New Data Types

 The compiler defines a only property and a only backing field for each specified name

read- Project new forms of data from an existing data source

Trang 40

Reversing Result Sets

 Using the generic Reverse<T>() method of the

Enumerable type

Trang 41

Sorting Expressions

 A query expression can take an orderby operator to sort items in the subset by a

specific value

 To view the results in a descending order, simply include the descending operator

var result = from item in container orderby value ascending/descending select item;

Trang 42

1 static void OrderedResults(Car[] myCars)

3 // Order all the cars by PetName.

4 var subset = from c in myCars orderby c.PetName select c;

5 Console.WriteLine( "Ordered by PetName:" );

6 foreach (Car c in subset)

10 // Now find the cars that are going less than 55 mph,

11 // and order by descending PetName

12 subset = from c in myCars

13 where c.Speed > 55 orderby c.PetName descending select c;

14 Console.WriteLine( "\nCars going faster than 55, ordered by PetName:" );

15 foreach (Car c in subset)

Trang 43

Finding Differences

obtaining a result set that determines the differences between

two IEnumerable<T> compatible containers

static void GetDiff()

var carDiff =(from c in myCars select c)

.Except(from c2 in yourCars select c2);

Console.WriteLine( "Here is what you don't have, but I do:" );

foreach ( string s in carDiff)

Console.WriteLine(s); // Prints Yugo.

}

Trang 44

 LINQ is a set of related technologies that attempts to

provide a single, symmetrical manner to interact with

diverse forms of data

 LINQ query expressions can return any number of result sets, it is common to make use of the var keyword to

represent the underlying data type

 LINQ query operators are simply shorthand notations for making calls on static members of the

System.Linq.Enumerable type

Trang 45

References

Ngày đăng: 02/08/2014, 09:20

TỪ KHÓA LIÊN QUAN