軟工班級課件_第1頁
軟工班級課件_第2頁
軟工班級課件_第3頁
軟工班級課件_第4頁
軟工班級課件_第5頁
已閱讀5頁,還剩42頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、 2009 Pearson Education, Inc. All rights reserved.19Introduction toLINQ and Generic Collections 2009 Pearson Education, Inc. All rights reserved.2OBJECTIVESIn this chapter you will learn: Basic LINQ concepts. How to query an array using LINQ. Basic .NET collections concepts. How to create and use a

2、generic List collection. How to write a generic method. How to query a generic List collection using LINQ. 2009 Pearson Education, Inc. All rights reserved.39.1 Introduction 9.2 Querying an Array Using LINQ 9.3 Introduction to Collections9.4 Querying a Generic Collection Using LINQ 2009 Pearson Educ

3、ation, Inc. All rights reserved.49.1 Introduction Although commonly used, arrays have limited capabilities. Lists are similar to arrays but provide additional functionality, such as dynamic resizing. Traditionally, programs used SQL queries to access a database. C#s new LINQ (Language-Integrated Que

4、ry) capabilities allow you to write query expressions that retrieve information from many data sources, not just databases. LINQ to Objects can be used to filter arrays and Lists, selecting elements that satisfy a set of conditions 2009 Pearson Education, Inc. All rights reserved.5Fig. 9.1 | LINQ us

5、age throughout the book. (Part 1 of 2.) Figure 9.1 shows where and how we use LINQ throughout the book to retrieve information from many data sources. 9.1 Introduction (Cont.) 2009 Pearson Education, Inc. All rights reserved.6Fig. 9.1 | LINQ usage throughout the book. (Part 2 of 2.)9.1 Introduction

6、(Cont.) A LINQ provider is a set of classes that implement LINQ operations and enable programs to interact with data sources to perform tasks such as projecting, sorting, grouping and filtering elements. 2009 Pearson Education, Inc. All rights reserved.7OutlineLINQWithSimpleLINQWithSimpleTypeArray.c

7、s TypeArray.cs ( 1 of 5 ) Figure 9.2 demonstrates querying an array of integers using LINQ.Fig. 9.2 | LINQ to Objects using an int array. (Part 1 of 5.)9.2 Querying an Array Using LINQ 2009 Pearson Education, Inc. All rights reserved.8OutlineLINQWithSimpleLINQWithSimpleTypeArray.cs TypeArray.cs ( 2

8、of 5 )Fig. 9.2 | LINQ to Objects using an int array. (Part 2 of 5.)A LINQ query begins with a fromfrom clause, which specifies a range variable (value) and the data source to query (values).If the condition in the wherewhere clause evaluates to true, the element is selected.The selectselect clause d

9、etermines what value appears in the results. The orderbyorderby clause sorts the query results in ascending order. 9.2 Querying an Array Using LINQ (Cont.) 2009 Pearson Education, Inc. All rights reserved.9OutlineLINQWithSimpleLINQWithSimpleTypeArray.cs TypeArray.cs ( 3 of 5 )Fig. 9.2 | LINQ to Obje

10、cts using an int array. (Part 3 of 5.)The descendingdescending modifier in the orderby clause sorts the results in descending order. 9.2 Querying an Array Using LINQ(Cont.) 2009 Pearson Education, Inc. All rights reserved.10OutlineLINQWithSimpleLINQWithSimpleTypeArray.cs TypeArray.cs ( 4 of 5 )Fig.

11、9.2 | LINQ to Objects using an int array. (Part 4 of 5.)9.2 Querying an Array Using LINQ (Cont.) 2009 Pearson Education, Inc. All rights reserved.11OutlineLINQWithSimpleLINQWithSimpleTypeArray.cs TypeArray.cs ( 5 of 5 )Fig. 9.2 | LINQ to Objects using an int array. (Part 5 of 5.)9.2 Querying an Arra

12、y Using LINQ (Cont.) 2009 Pearson Education, Inc. All rights reserved.129.2 Querying an Array Using LINQ (Cont.) Repetition statements that filter arrays focus on the steps required to get the results. This is called imperative programming. LINQ queries, however, specify the conditions that selected

13、 elements must satisfy. This is known as declarative programming. The System.Linq namespace contains the LINQ to Objects provider. 2009 Pearson Education, Inc. All rights reserved.139.2 Querying an Array Using LINQ (Cont.) A LINQ query begins with a from clause, which specifies a range variable (val

14、ue) and the data source to query (values). The range variable represents each item in the data source,much like the control variable in a foreach statement. If the condition in the where clausewhere clause evaluates to true,the element is selected. A predicate is an expression that takes an element

15、of a collection and returns true or false by testing a condition on that element. The select clause determines what value appears in the results. 2009 Pearson Education, Inc. All rights reserved.149.2 Querying an Array Using LINQ (Cont.) The Display method takes an IEnumerable object as an argument.

16、 The type int enclosed in angle brackets after the type name indicates that this IEnumerable may only hold integers. Any type may be used as a type argument in this mannertypes can be passed as arguments to generic types just as objects are passed as arguments to methods. 2009 Pearson Education, Inc

17、. All rights reserved.159.2 Querying an Array Using LINQ (Cont.) Interfaces define and standardize the ways in which people and systems can interact with one another. A C# interface describes a set of methods that can be called on an object. A class that implements an interface must define each meth

18、od in the interface with a signature identical to the one in the interface definition. 2009 Pearson Education, Inc. All rights reserved.169.2 Querying an Array Using LINQ (Cont.) The IEnumerable interface describes the functionality of any object that can be iterated over and thus offers methods to

19、access each element. Arrays and collections already implement the IEnumerable interface. A LINQ query returns an object that implements the IEnumerable interface. With LINQ, the code that selects elements and the code that displays them are kept separate, making the code easier to understand and mai

20、ntain. 2009 Pearson Education, Inc. All rights reserved.179.2 Querying an Array Using LINQ (Cont.) The orderby clause sorts the query results in ascending order. The descending modifier in the orderby clause sorts the results in descending order. Any value that can be compared with other values of t

21、he same type may be used with the orderby clause. 2009 Pearson Education, Inc. All rights reserved.189.2 Querying an Array Using LINQ (Cont.) LINQ is not limited to querying arrays of primitive types such as integers. Comparable types in .NET are those that implement the IComparable. All built-in ty

22、pes, such as string, int and double implement IComparable. 2009 Pearson Education, Inc. All rights reserved.19OutlineEmployee.cs Employee.cs ( 1 of 3 ) Figure 9.3 presents the Employee class.Fig. 9.3 | Employee class with FirstName, LastName and MonthlySalary properties. (Part 1 of 3.)9.2 Querying a

23、n Array Using LINQ (Cont.) 2009 Pearson Education, Inc. All rights reserved.20OutlineEmployee.cs Employee.cs ( 2 of 3 )Fig. 9.3 | Employee class with FirstName, LastName and MonthlySalary properties. (Part 2 of 3.)9.2 Querying an Array Using LINQ (Cont.) 2009 Pearson Education, Inc. All rights reser

24、ved.21OutlineEmployee.cs Employee.cs ( 3 of 3 )Fig. 9.3 | Employee class with FirstName, LastName and MonthlySalary properties. (Part 3 of 3.)9.2 Querying an Array Using LINQ (Cont.) 2009 Pearson Education, Inc. All rights reserved.22OutlineLINQWithArrayOfLINQWithArrayOfObjects.cs Objects.cs ( 1 of

25、5 )Fig. 9.4 | LINQ to Objects using an array of Employee objects. (Part 1 of 5.) Figure 9.4 uses LINQ to query an array of Employee objects. 9.2 Querying an Array Using LINQ (Cont.) 2009 Pearson Education, Inc. All rights reserved.23OutlineFig. 9.4 | LINQ to Objects using an array of Employee object

26、s. (Part 2 of 5.)LINQWithArrayOfLINQWithArrayOfObjects.cs Objects.cs ( 2 of 5 )A where clause can access the properties of the range variable. 2009 Pearson Education, Inc. All rights reserved.24OutlineFig. 9.4 | LINQ to Objects using an array of Employee objects. (Part 3 of 5.)LINQWithArrayOfLINQWit

27、hArrayOfObjects.cs Objects.cs ( 3 of 5 )An orderby clause can sort the results according to multiple properties, specified in a comma-separated list. The query results AnyAny method returns true if there is at least one element, and false if there are no elements. The query results FirstFirst method

28、 (line 45) returns the first element in the result The select clause can be used to select a member of the range variable rather than the range variable itself. 2009 Pearson Education, Inc. All rights reserved.25OutlineFig. 9.4 | LINQ to Objects using an array of Employee objects. (Part 4 of 5.)LINQ

29、WithArrayOfLINQWithArrayOfObjects.cs Objects.cs ( 4 of 5 )The DistinctDistinct method removes duplicate elements, causing all elements in the result to be unique. The select clause can create a new object of anonymous type (a type with no name), which the compiler generates for you based on the prop

30、erties listed in the curly braces (). To define a generic method, you must specify a type parameter list which contains one or more type parameters separated by commas. 2009 Pearson Education, Inc. All rights reserved.26OutlineFig. 9.4 | LINQ to Objects using an array ofEmployee objects. (Part 5 of

31、5.)LINQWithArrayOfLINQWithArrayOfObjects.cs Objects.cs ( 5 of 5 ) 2009 Pearson Education, Inc. All rights reserved.279.2 Querying an Array Using LINQ (Cont.) A where clause can access the properties of the range variable. The conditional AND (&) operator can be used to combine conditions. An ord

32、erby clause can sort the results according to multiple properties, specified in a comma-separated list. 2009 Pearson Education, Inc. All rights reserved.289.2 Querying an Array Using LINQ (Cont.) The query results AnyAny method returns true if there is at least one element, and false if there are no

33、 elements. The query results FirstFirst method (line 45) returns the first element in the result. The CountCount method of the query result returns the number of elements in the results. The select clause can be used to select a member of the range variable rather than the range variable itself. The

34、 Distinct methodDistinct method removes duplicate elements, causing all elements in the result to be unique. 2009 Pearson Education, Inc. All rights reserved.299.2 Querying an Array Using LINQ (Cont.) The select clause can create a new object of anonymous type (a type with no name), which the compil

35、er generates for you based on the properties listed in the curly braces (). By default, the name of the property being selected is used as the propertys name in the result. You can specify a different name for the property inside the anonymous type definition. 2009 Pearson Education, Inc. All rights

36、 reserved.309.2 Querying an Array Using LINQ (Cont.) Implicitly typed local variables allow you to use anonymous types because you do not have to explicitly state the type when declaring such variables. When the compiler creates an anonymous type, it automatically generates a ToString method that re

37、turns a string representation of the object. 2009 Pearson Education, Inc. All rights reserved.319.2 Querying an Array Using LINQ (Cont.) Generic methods enable you to create a single method definition that can be called with arguments of many types. To define a generic method, you must specify a typ

38、e parameter list which contains one or more type parameters separated by commas. A type parameter is a placeholder for a type argument. They can be used to declare return types, parameter types and local variable types in generic method declarations. 2009 Pearson Education, Inc. All rights reserved.

39、329.2 Querying an Array Using LINQ (Cont.) Can only appear once in the type-parameter list. Can appear more than once in the methods parameter listand body Can be the methods return type Type-parameter names must match throughout a method, but need not be unique among different generic methods.Commo

40、n Programming Error 9.1If you forget to include the type-parameter list when declaring a generic method, the compiler will not recognize the type-parameter names when theyre encountered in the method, causing compilation errors. 2009 Pearson Education, Inc. All rights reserved.33 The .NET Framework

41、Class Library provides collections, which are used to store groups of related objects. Collections provide efficient methods that organize, store and retrieve your data without requiring knowledge of how the data is being stored. The collection class ListList (from namespace System.Collections.Gener

42、ic) does not need tobe reallocated to change its size. 9.3 Introduction to Collections 2009 Pearson Education, Inc. All rights reserved.34Fig. 9.5 | Some methods and properties of class List. (Part 1 of 2.)9.3 Introduction to Collections (Cont.) List is called a generic class because it can be used

43、with anytype of object. T is a placeholder for the type of the objects stored in the list. Figure 9.5 shows some common methods and properties of class List. 2009 Pearson Education, Inc. All rights reserved.35Fig. 9.5 | Some methods and properties of class List. (Part 2 of 2.)9.3 Introduction to Col

44、lections (Cont.) 2009 Pearson Education, Inc. All rights reserved.36OutlineListCollection.cs ListCollection.cs ( 1 of 4 )Fig. 9.6 | Generic List collection demonstration. (Part 1 of 4.) Figure 9.6 demonstrates dynamically resizing a List object. The InsertInsert method inserts a new element at the s

45、pecified position. The AddAdd method appends its argument to the end of the List. 9.3 Introduction to Collections (Cont.) 2009 Pearson Education, Inc. All rights reserved.37OutlineListCollection.cs ListCollection.cs ( 2 of 4 )Fig. 9.6 | Generic List collection demonstration. (Part 2 of 4.)Lists can

46、be indexed like arrays by placing the index in square brackets after the List variables name. The RemoveRemove method is used to remove the first instance of an element with a specific value. RemoveAtRemoveAt removes the element at the specified index; all elements above that index are shifted down

47、by one. 9.3 Introduction to Collections (Cont.) 2009 Pearson Education, Inc. All rights reserved.38OutlineListCollection.cs ListCollection.cs ( 3 of 4 )Fig. 9.6 | Generic List collection demonstration. (Part 3 of 4.)The ContainsContains method returns true if the element is found in the List, and fa

48、lse otherwise. The CapacityCapacity property indicates how many items the List can hold without growing. 9.3 Introduction to Collections (Cont.) 2009 Pearson Education, Inc. All rights reserved.39OutlineListCollection.cs ListCollection.cs ( 4 of 4 )Fig. 9.6 | Generic List collection demonstration. (

49、Part 4 of 4.)9.3 Introduction to Collections (Cont.) 2009 Pearson Education, Inc. All rights reserved.40 The AddAdd method appends its argument to the end of the List. The InsertInsert method inserts a new element at the specified position. The first argument is an indexas with arrays, collection in

50、dices start at zero. The second argument is the value that is to be inserted at the specified index. All elements at the specified index and above are shifted upby one position. 9.3 Introduction to Collections (Cont.) 2009 Pearson Education, Inc. All rights reserved.41 The CountCount property return

51、s the number of elements currently in the List. Lists can be indexed like arrays by placing the index in square brackets after the List variables name. The RemoveRemove method is used to remove the first instanceof an element with a specific value. If no such element is in the List, Remove does noth

52、ing. RemoveAtRemoveAt removes the element at the specified index;all elements above that index are shifted down by one.9.3 Introduction to Collections (Cont.) 2009 Pearson Education, Inc. All rights reserved.42 The ContainsContains method returns true if the element is found in the List, and false o

53、therwise. Contains compares its argument to each element of the List in order, so using Contains on a large List is inefficient. The CapacityCapacity property indicates how many items the List can hold without growing. List is implemented using an array behind the scenes. When the List grows, it must create a larger inte

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論