Visual C# 2012 How to Program by Pe ars on Ed uc ati on, Inc. All Ri ght s Re ser ve d.

Similar documents
High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Querying In-Memory Data by Using Query Expressions

Previous C# Releases. C# 3.0 Language Features. C# 3.0 Features. C# 3.0 Orcas. Local Variables. Language Integrated Query 3/23/2007

CS313D: ADVANCED PROGRAMMING LANGUAGE

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

System.Collections.Generic.SortedSet: who needs another collection anyway?

IMPLEMENTING THE LINQ QUERY LANGUAGE INTO THE C++ PROGRAMMING LANGUAGE USING A PREPROCESSOR

Chapter 2, Part III Arithmetic Operators and Decision Making

LINQ Language-Integrated Query Introduction

CHAPTER 1: INTRODUCING C# 3

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

by Pearson Education, Inc. All Rights Reserved. 2

Advanced Programming C# Lecture 10. dr inż. Małgorzata Janik

.NET Database Technologies. Entity Framework: Queries and Transactions

CSC Web Programming. Introduction to SQL

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

Chapter 6: Using Arrays

With examples in F# and C# SAMPLE CHAPTER. Tomas Petricek. WITH Jon Skeet FOREWORD BY MADS TORGERSEN MANNING

C Programming for Engineers Functions

Learning C# 3.0. Jesse Liberty and Brian MacDonald O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Microsoft Visual C# Step by Step. John Sharp

Functional programming in C#

by Pearson Education, Inc. All Rights Reserved. 2

Arrays, Strings and Collections

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Visual C# Instructor s Manual Table of Contents

Introduction to C# Applications Pearson Education, Inc. All rights reserved.

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

by Pearson Education, Inc. All Rights Reserved. 2

Why do you want to know about functional programming?

Lecture 2 Tao Wang 1

LINQ Queries. Getting Started.

PHP by Pearson Education, Inc. All Rights Reserved.

BBM 102 Introduction to Programming II Spring Inheritance

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

ARG! Language Reference Manual

LAB: INTRODUCTION TO FUNCTIONS IN C++

Seminar 11 week 11 (11-15 December 2017)

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Functions. Systems Programming Concepts


Chapter 2: Functions and Control Structures

Object-Oriented Programming in C# (VS 2015)

CS313D: ADVANCED PROGRAMMING LANGUAGE

Introduction To C#.NET

Learn to Love Lambdas

egrapher Language Reference Manual

Object-Oriented Programming in C# (VS 2012)

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Learn to Love Lambdas

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

C# in Depth THIRD EDITION

Programming in Visual Basic with Microsoft Visual Studio 2010

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

LinQ Why we have to teach functional programmming

PROGRAMMING IN VISUAL BASIC WITH MICROSOFT VISUAL STUDIO Course: 10550A; Duration: 5 Days; Instructor-led

Getting started with C++ (Part 2)

LINQ. One of the most hyped innovations in.net 3.5 is LINQ (Language Integrated Query), a set of language. LINQ Basics CHAPTER 13

DEVELOPING DATABASE APPLICATIONS (INTERMEDIATE MICROSOFT ACCESS, X405.5)

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Variable Scope The Main() Function Struct Functions Overloading Functions Using Delegates Chapter 7: Debugging and Error Handling Debugging in Visual

BTE2313. Chapter 2: Introduction to C++ Programming

Data about data is database Select correct option: True False Partially True None of the Above

LINQ in Action. Fabrice Marguerie Steve Eichert Jim Wooley. Chapter 3. Copyright 2008 Manning Publications

Some LINQ Experience Tuesday, 22 September :48 - Last Updated Tuesday, 22 September :29

Chapter 17. Fundamental Concepts Expressed in JavaScript


\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Contents. Before You Begin. Object Technology: A Brief Review

C# in Depth SECOND EDITION JON SKEET. MANNING Greenwich (74 w. long.)

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

Arrays, Pointers and Memory Management

DOWNLOAD PDF VISUAL BASIC 2008 EXAMPLES

COPYRIGHTED MATERIAL. Contents. Part I: C# Fundamentals 1. Chapter 1: The.NET Framework 3. Chapter 2: Getting Started with Visual Studio

CSC 330 Object-Oriented

ArrayList. Introduction. java.util.arraylist

Getting Information from a Table

Chapter 2, Part I Introduction to C Programming

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

IT 374 C# and Applications/ IT695 C# Data Structures

Iterators & Generators

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

Learn C# Errata. 3-9 The Nullable Types The Assignment Operators


142

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Introduction to LINQ. Paul Litwin Collaborative Data Services (CDS)

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Comprehension. Thomas Schwarz, SJ Marquette University

Control Structures. CIS 118 Intro to LINUX

Fundamentals of Programming Session 24

By Jason Roberts. Foreword by Daniel Jebaraj

Transcription:

Visual C# 2012 How to Program 1 99 2-20 14 by Pe ars on Ed uc ati on, Inc. All Ri ght s Re ser ve d.

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

Although commonly used, arrays have limited capabilities. A List is similar to an array but provides additional functionality, such as dynamic resizing. A language called SQL is the international standard used to perform queries (i.e., to request information that satisfies given criteria) and to manipulate data. C# s new LINQ (Language-Integrated Query) capabilities allow you to write query expressions that retrieve information from a variety of data sources, not just databases. LINQ to Objects can be used to filter arrays and Lists, selecting elements that satisfy a set of conditions 1992-2014 by Pearson Education, Inc. All

Figure 9.1 shows where and how we use LINQ throughout the book to retrieve information from many data sources. 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. 1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

Figure 9.2 demonstrates querying an array of integers using LINQ. 1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

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 elements must satisfy. This is known as declarative programming. The System.Linq namespace contains the LINQ to Objects provider. 1992-2014 by Pearson Education, Inc. All

You can declare a local variable and let the compiler infer the variable s type based on the variable s initializer. The var keyword is used in place of the variable s type when declaring the variable. A LINQ query begins with a from clause, which specifies a range variable (value) 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 clause evaluates to true, the element is selected. 1992-2014 by Pearson Education, Inc. All

A predicate is an expression that takes an element 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. 1992-2014 by Pearson Education, Inc. All

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 the same type may be used with the orderby clause. 1992-2014 by Pearson Education, Inc. All

The IEnumerable<T> interface describes the functionality of any object that can be iterated over and thus offers members to access each element. Arrays and collections already implement the IEnumerable<T> interface. A LINQ query returns an object that implements the IEnumerable<T> interface. With LINQ, the code that selects elements and the code that displays them are kept separate, making the code easier to understand and maintain. 1992-2014 by Pearson Education, Inc. All

LINQ is not limited to querying arrays of simple types such as integers. Comparable types in.net are those that implement the IComparable<T>. All built-in types, such as string, int and double implement IComparable<T>. Figure 9.3 presents the Employee class. Figure 9.4 uses LINQ to query an array of Employee objects. 1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

A where clause can access the properties of the range variable. The conditional AND (&&) operator can be used to combine conditions. An orderby clause can sort the results according to multiple properties, specified in a comma-separated list. 1992-2014 by Pearson Education, Inc. All

The query result s Any method returns true if there is at least one element, and false if there are no elements. The query result s First method returns the first element in the result. The Count 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 Distinct method removes duplicate elements, causing all elements in the result to be unique. 1992-2014 by Pearson Education, Inc. All

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 properties listed in the curly braces ({}). By default, the name of the property being selected is used as the property s name in the result. You can specify a different name for the property inside the anonymous type definition. 1992-2014 by Pearson Education, Inc. All

The preceding query is an example of a projection it performs a transformation on the data. The transformation creates new objects containing only the FirstName and Last properties. Transformations can also manipulate the data. For example, you could give all employees a 10% raise by multiplying their MonthlySalary properties by 1.1. 1992-2014 by Pearson Education, Inc. All

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 returns a string representation of the object. 1992-2014 by Pearson Education, Inc. All

The.NET Framework Class Library provides several classes, called collections, used to store groups of related objects. These classes provide efficient methods that organize, store and retrieve your data without requiring knowledge of how the data is being stored. The collection class List<T> (from namespace System.Collections.Generic) does not need to be reallocated to change its size. 1992-2014 by Pearson Education, Inc. All

List<T> is called a generic class because it can be used with any type 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<T>.

1992-2014 by Pearson Education, Inc. All

Figure 9.6 demonstrates dynamically resizing a List object.

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

The Add method appends its argument to the end of the List. The Insert method inserts a new element at the specified position. The first argument is an index as with arrays, collection indices start at zero. The second argument is the value that s to be inserted at the specified index. The indices of elements at the specified index and above increase by one. 1992-2014 by Pearson Education, Inc. All

The Count property returns the number of elements currently in the List. Lists can be indexed like arrays by placing the index in square brackets after the List variable s name. The Remove method is used to remove the first instance of an element with a specific value. If no such element is in the List, Remove does nothing. RemoveAt removes the element at the specified index; the indices of all elements above that index decrease by one. 1992-2014 by Pearson Education, Inc. All

The Contains method returns true if the element is found in the List, and false otherwise. Contains compares its argument to each element of the List in order, so using Contains on a large List is inefficient. The Capacity property indicates how many items the List can hold without having to grow. When the List grows, it must create a larger internal array and copy each element to the new array. A List grows only when an element is added and there is no space for the new element. 1992-2014 by Pearson Education, Inc. All

You can use LINQ to Objects to query Lists just as arrays. In Fig. 9.7, a List of strings is converted to uppercase and searched for those that begin with "R". 1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

1992-2014 by Pearson Education, Inc. All

LINQ s let clause can be used to create a new range variable to store a temporary result for use later in the LINQ query. The string method ToUpper to converts a string to uppercase. The string method StartsWith performs a case sensitive comparison to determine whether a string starts with the string received as an argument. 1992-2014 by Pearson Education, Inc. All

LINQ uses deferred execution the query executes only when you access the results, not when you define the query. LINQ extension methods ToArray and ToList immediately execute the query on which they are called. These methods execute the query only once, improving efficiency. 1992-2014 by Pearson Education, Inc. All