Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann

Slides:



Advertisements
Similar presentations
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Advertisements

Generics, Lists, Interfaces
LINQ and Collections An introduction to LINQ and Collections.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
Java Script Session1 INTRODUCTION.
Java Review Interface, Casting, Generics, Iterator.
.NET 3.5 – Mysteries. NetFx Evolution NetFx 1.0 C# 1.0, VB 7.0, VS.NET NetFx 1.1 C# 1.1, VB 7.1, VS 2003 NetFx 2.0 C# 2.0, VB 8.0, VS 2005 NetFx 3.0 C#
1 COS 425: Database and Information Management Systems XML and information exchange.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.
A tour of new features introducing LINQ. Agenda of LINQ Presentation We have features for every step of the way LINQ Fundamentals Anonymous Functions/Lambda.
XML files (with LINQ). Introduction to LINQ ( Language Integrated Query ) C#’s new LINQ capabilities allow you to write query expressions that retrieve.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx.
LINQ Programming in C# LINQ CSE Prof. Roger Crawfis.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
 Introduction  What is LINQ  Syntax  How to Query  Example Program.
Object Oriented Programming Classes and Objects Dr. Mike Spann
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
Working with the XML Document Object Model ©NIITeXtensible Markup Language/Lesson 7/Slide 1 of 44 Objectives In this lesson, you will learn to: *Identify.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Based on material from Telerik Corporation.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
VB and C# Programming Basics. Overview Basic operations String processing Date processing Control structures Functions and subroutines.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
Chapter 18 Java Collections Framework
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Object Oriented Programming Classes and Objects Dr. Mike Spann
 Although VERY commonly used, arrays have limited capabilities  A List is similar to an array but provides additional functionality, such as dynamic.
Introduction to LINQ Chapter 11. Introduction Large amounts of data are often stored in a database—an organized collection of data. A database management.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
EE2E1. JAVA Programming Lecture 3 Inheritance. Contents Base classes and derived classes Base classes and derived classes Protected scope Protected scope.
Chapter 4 Introduction to Classes, Objects, Methods and strings
XML and Database.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
LINQ Language Integrated Query LINQ1. LINQ: Why and what? Problem Many data sources: Relational databases, XML, in-memory data structures, objects, etc.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Chapter 11.  Large amounts of data are often stored in a database—an organized collection of data.  A database management system (DBMS) provides mechanisms.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Lecture 10 Collections Richard Gesick.
The need for Programming Languages
Introduction to LINQ and Generic Collections
CIS 200 Test 01 Review.
Visual Basic 2010 How to Program
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Introduction to LINQ Chapter 11 10/28/2015 Lect 4 CT1411.
Introduction to LINQ Chapter 11.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections UTPA – Fall 2012 This set of slides is revised from lecture slides.
Introduction to Data Structure
Fundaments of Game Design
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
5. OOP OOP © 2003 Microsoft.
Presentation transcript:

Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann

Contents Introduction to collection objects and LINQ Introduction to collection objects and LINQ Querying an array Querying an array Generic collections Generic collections Querying a generic collection Querying a generic collection LINQ to XML LINQ to XML Summary Summary

Introduction to collection objects and LINQ.NET provides a set of pre-packaged data structures known as collections.NET provides a set of pre-packaged data structures known as collections  They have been carefully designed to ensure robust and efficient performance  Array  Efficient random access but inefficient to resize it  List  Enables dynamic resizing but not random access

Introduction to collection objects and LINQ In addition, C# provides a mechanism for querying collections known as LINQ In addition, C# provides a mechanism for querying collections known as LINQ  Language Integrated Query LINQ enables access to collections (and databases!) using query expressions which are similar to SQL queries LINQ enables access to collections (and databases!) using query expressions which are similar to SQL queries  This allows the retrieval of information from a wide variety of data sources

Introduction to collection objects and LINQ We will primarily look at LINQ to Objects We will primarily look at LINQ to Objects  Enables the querying on collection objects.NET also provides LINQ providers for :.NET also provides LINQ providers for :  LINQ to SQL  For querying databases  LINQ to XML  For querying xml documents

Querying an array We can design a simple LINQ query which filters the contents of an array We can design a simple LINQ query which filters the contents of an array  Applying the query to the array causes all the values of the array which meet a certain criteria to be extracted  But the query doesn’t say how the iteration through the array is performed  All the necessary code is generated by the compiler, the query just specifies the criteria

Querying an array A simple query object comprises from, where and select clauses A simple query object comprises from, where and select clauses  Also, we can make use of the keyword var which is an implicit type  Essentially a strongly typed local variable but the type is determined by the compiler  In this case the type of the array is determined by the data source type var filteredArray = from......// range variable and data source where.....// boolean expression select.....// which value appears in the results

using System; using System.Collections.Generic; using System.Linq; class LINQtoArray { static void Main(string[] args) { int[] array = { 2, 6, 4, 12, 7, 8, 9, 13, 2 }; var filteredArray =// LINQ query from element in array where element < 7 select element; PrintArray(filteredArray, "All values less than 7:"); } public static void PrintArray(IEnumerable arr, string message) { Console.Write("{0}",message); foreach (var element in arr) Console.Write(" {0}", element); Console.WriteLine(); }

Querying an array IEnumerable is an interface implemented by arrays and collections IEnumerable is an interface implemented by arrays and collections It is a generic type It is a generic type  We replace the T by a real type (such as an int)  More on this later

Querying an array We can add the orderby (descending) clause to our query to sort our filtered array into ascending (descending) order We can add the orderby (descending) clause to our query to sort our filtered array into ascending (descending) order var filteredArray = from......// range variable and data source where.....// boolean expression orderby..... (descending) // sort select.....// which value appears in the resul ts

using System; using System.Collections.Generic; using System.Linq; class LINQtoArray { static void Main(string[] args) { int[] array = { 2, 6, 4, 12, 7, 8, 9, 13, 2 }; var filteredArray =// LINQ query from element in array where element < 7 select element; PrintArray(filteredArray, "All values less than 7:"); var orderedFilteredArray = from element in filteredArray orderby element select element; PrintArray(orderedFilteredArray, "All values less than 7 and sorted:"); } public static void PrintArray(IEnumerable arr, string message) {……} }

Querying an array It's important to understand a feature of LINQ known as deferred execution It's important to understand a feature of LINQ known as deferred execution  The result of a LINQ query expression is not a sequence or collection of objects but a query object  It represents the commands needed to execute the query  The query does not execute until the program requests data from the query object  Deferred execution is a powerful feature of LINQ as it allows applications to pass queries around as data  In our simple example, the query is not run until it is passed to the PrintArray method

Querying an array We can use LINQ to query an array of user defined objects or strings We can use LINQ to query an array of user defined objects or strings We must be careful when using orderby We must be careful when using orderby  The objects must be comparable  Comparable types in.NET implement the IComparable interface  Built in primitive types automatically implement IComparable  Built in primitive types automatically implement IComparable  The ‘T’ is a parameterised type  We will look at these in more detail later For example we can query an array of StudentInfo objects For example we can query an array of StudentInfo objects

class StudentInfo { public StudentInfo(string ln, string fn, int id, string a) { lastName = ln; firstName = fn; idNumber = id; address = a; } public override string ToString() { return firstName+" "+lastName+" "+idNumber+" " +address; } public string FirstName { get { return firstName; } } public string LastName { get { return lastName; } } public string Address { get { return address; } } public int ID { get { return idNumber; } } private string firstName,lastName; private int idNumber; private string address; }

Querying an array We can filter the array by ID number We can filter the array by ID number  Simply get the ID property of the range variable Also we can sort the names into last name order and then first name order using orderby Also we can sort the names into last name order and then first name order using orderby  This uses the fact that the string type implements IComparable  This uses the fact that the string type implements IComparable

public class LINQtoObjectArray { static void Main(string[] args) { StudentInfo[] students ={ new StudentInfo("Smith", "John", 12345, "5 Bournbrook Rd"), new StudentInfo("Brown", "Alan", 23412, "Dawlish Rd"), new StudentInfo("Smith","Colin", 41253, "23 Bristol Rd"), new StudentInfo("Hughes", "Richard", 52314, "18 Prichatts Rd"), new StudentInfo("Murphy", "Paul", 16352, "37 College Rd") }; // Filter a range of ID numbers var idRange= from s in students where s.ID>19999 && s.ID<=49999 select s; PrintArray(idRange,"Students with ID in Range 2000 to 4999"); // Order by last name and then first name var nameSorted = from s in students orderby s.LastName, s.FirstName select s; PrintArray(nameSorted, "Students sorted in last name, first name order"); } public static void PrintArray (IEnumerable arr, string message) {…} }

Querying an array

We can use LINQ to sort the array of StudentInfo objects by implementing the IComparable interface We can use LINQ to sort the array of StudentInfo objects by implementing the IComparable interface  We simply need to implement the CompareTo() method  If this isn’t done when we try and sort an array of objects using LINQ, a runtime exception is generated

Querying an array class StudentInfo : IComparable { public StudentInfo(string ln, string fn, int id, string a) { lastName = ln; firstName = fn; idNumber = id; address = a; } public int CompareTo(object obj) { StudentInfo s = (StudentInfo)obj; if (s.ID < ID) return 1; else if (s.ID > ID) return -1; else return 0; }. private string firstName,lastName; private int idNumber; private string address; }

Querying an array public class LINQtoObjectArray { static void Main(string[] args) { StudentInfo[] students ={ new StudentInfo("Smith", "John", 12345, "5 Bournbrook Rd"), new StudentInfo("Brown", "Alan", 23412, "Dawlish Rd"), new StudentInfo("Smith","Colin", 41253, "23 Bristol Rd"), new StudentInfo("Hughes", "Richard", 52314, "18 Prichatts Rd"), new StudentInfo("Murphy", "Paul", 16352, "37 College Rd") }; // Order by ID var IDSorted = from s in students orderby s select s; PrintArray(IDSorted, "Students sorted in ID order"); public static void PrintArray (IEnumerable arr, string message) {…} }

Querying an array

LINQ defines a number of extension methods of IEnumerable LINQ defines a number of extension methods of IEnumerable  Extension methods extend the functionality of existing classes (including classes in the FCL)  Any() Checks to see if the container has any membersChecks to see if the container has any members  Count() Returns a count of the number of membersReturns a count of the number of members  First(), Last() Returns the first and last members of the containerReturns the first and last members of the container  Distinct() Removes duplicate membersRemoves duplicate members

public class LINQtoObjectArray { static void Main(string[] args) { StudentInfo[] students ={ new StudentInfo("Smith", "John", 12345, "5 Bournbrook Rd"), new StudentInfo("Brown", "Alan", 23412, "Dawlish Rd"), new StudentInfo("Smith","Colin", 41253, "23 Bristol Rd"), new StudentInfo("Hughes", "Richard", 52314, "18 Prichatts Rd"), new StudentInfo("Murphy", "Paul", 16352, "37 College Rd") }; // Order by last name and then first name var nameSorted = from s in students orderby s.LastName, s.FirstName select s; PrintArray(nameSorted, "Students sorted in last name, first name order"); Console.WriteLine("There are " + students.Count() + " students"); if (nameSorted.Any()) { Console.WriteLine("First in list " + nameSorted.First().ToString()); Console.WriteLine("Last in list " + nameSorted.Last().ToString()); }

Querying an array

Generic collections In our example programs so far we have already seen a generic method PrintArray In our example programs so far we have already seen a generic method PrintArray  This function outputs the string representation of the elements of an array  Its full declarations is :  PrintArray takes any type which implements the IEnumerable interface  Thus an IEnumerable object of any type can be passed to the method public static void PrintArray (IEnumerable arr,string message)

Generic collections The compiler infers the type T from the actual call to PrintArray The compiler infers the type T from the actual call to PrintArray public class LINQtoObjectArray { static void Main(string[] args) { StudentInfo[] students ={………}; // Order by last name and then first name var nameSorted = from s in students orderby s.LastName, s.FirstName select s; // PrintArray called here PrintArray(nameSorted, "Students sorted in last name, first name order");. }

Generic collections Also the compiler determines whether the operations in the method body can be performed on any type the T represents Also the compiler determines whether the operations in the method body can be performed on any type the T represents class MyClass { public static void PrintArray(IEnumerable arr, string message) { Console.Write("{0}",message); // Requires a ToString() override method for non built in types foreach (var element in arr) Console.Write(" {0}", element); Console.WriteLine(); }

Generic collections Collections store groups of objects Collections store groups of objects We are all familiar with arrays We are all familiar with arrays  Arrays don’t resize dynamically but do so when the Resize() method is called The collection class List dynamically resizes when objects are inserted The collection class List dynamically resizes when objects are inserted  It’s known as a generic class because real classes are instantiated by providing actual types in place of T  List, List, List etc

Generic collections Method or property Description Add Adds an element to the end of the List Capacity Property that gets or sets the number of elements a List can store Clear Removes all the elements from the List Contains Returns true if the List contains the specified element; otherwise, returns false Count Property that returns the number of elements stored in the List IndexOf Returns the index of the first occurrence of the specified value in the List Insert Inserts an element at the specified index Remove Removes the first occurrence of the specified value RemoveAt Removes the element at the specified index RemoveRange Removes a specified number of elements starting at a specified index Sort Sorts the List TrimExcess Sets the Capacity of the List to the number of elements the List currently contains (Count)

Generic collections We can create a list of StudentInfo objects and add items (to the end of the list) and insert items (anywhere in the list) We can create a list of StudentInfo objects and add items (to the end of the list) and insert items (anywhere in the list) We can display the list using exactly the same generic function as for displaying an array We can display the list using exactly the same generic function as for displaying an array We could manipulate the list using the methods shown in the table We could manipulate the list using the methods shown in the table

public class LINQtoList { static void Main(string[] args) { StudentInfo[] students ={ new StudentInfo("Smith", "John", 12345, "5 Bournbrook Rd"), new StudentInfo("Brown", "Alan", 23412, “Dawlish Rd"), new StudentInfo("Smith","Colin", 41253, "23 Bristol Rd"), new StudentInfo("Hughes", "Richard", 52314, "18 Prichatts Rd"), new StudentInfo("Murphy", "Paul", 16352, "37 College Rd") }; List studentList = new List (); studentList.Add(students[0]); studentList.Add(students[1]); studentList.Add(students[2]); studentList.Insert(2, students[3]); PrintList(studentList, "Student list:"); } public static void PrintList (IEnumerable arr, string message) { Console.WriteLine("{0}", message); foreach (T element in arr) Console.WriteLine(" {0}", element); Console.WriteLine(); }

Generic collections

Querying a generic collection LINQ to Objects can query lists (and any other collection) in much the same way as querying an array LINQ to Objects can query lists (and any other collection) in much the same way as querying an array For example, we could sort our list of students after first converting their surnames into upper case For example, we could sort our list of students after first converting their surnames into upper case  The query makes use of the let clause which creates a new range variable  Enables a temporary result to be stored for later use in the query

public class LINQtoList { static void Main(string[] args) { StudentInfo[] students ={ new StudentInfo("Smith", "John", 12345, "5 Bournbrook Rd"), new StudentInfo("Brown", "Alan", 23412, “Dawlish Rd"), new StudentInfo("Smith","Colin", 41253, "23 Bristol Rd"), new StudentInfo("Hughes", "Richard", 52314, "18 Prichatts Rd"), new StudentInfo("Murphy", "Paul", 16352, "37 College Rd") }; List studentList = new List (); studentList.Add(students[0]); studentList.Add(students[1]); studentList.Add(students[2]); studentList.Insert(2, students[3]); var orderedUpperCaseList = from student in studentList let upperCaseName = student.LastName.ToUpper() orderby upperCaseName select upperCaseName; PrintList(orderedUpperCaseList, "Ordered student list:"); } public static void PrintList (IEnumerable arr, string message) {…} }

Querying a generic collection

LINQ to XML XML (Extensible Markup Language) is a widely supported standard for describing virtually any kind of information data XML (Extensible Markup Language) is a widely supported standard for describing virtually any kind of information data It is a used primarily to describe data which is shared between applications across a network It is a used primarily to describe data which is shared between applications across a network It comprises elements delineated by a start and end tag and data It comprises elements delineated by a start and end tag and data XML files are simple text files with no formatting structure XML files are simple text files with no formatting structure LINQ contains a number of classes for parsing and manipulating XML files LINQ contains a number of classes for parsing and manipulating XML files

LINQ to XML A simple XML document can describe student information data A simple XML document can describe student information data John Smith February Electrical Engineering MEng 21 Bristol Road

LINQ to XML XML documents can be stored as a tree structure in memory XML documents can be stored as a tree structure in memory  Known as a Document Object Model (DOM) tree  Each element in the XML document is represented by a tree node  XML parsers can create such a tree from the XML file  The tree can then be manipulated programmatically by high level languages

LINQ to XML studentInfo name birthday courseInfo address code courseTitle degree

LINQ to XML Namespace System.Xml.Linq contains classes used to manipulate a DOM Namespace System.Xml.Linq contains classes used to manipulate a DOM  XDocument represents an entire XML document  XElement represents a tree node  XElement.Name property allows the name of the node element to be get or set (including namespace)  XElement.Name.LocalName is the name without the namespace prefix

LINQ to XML We can write a simple program which uses these classes to read in our XML document and display individual elements We can write a simple program which uses these classes to read in our XML document and display individual elements  Uses the HasElements property to determine if an XElement object has children and Elements() method to obtain the children  Uses the Value property to return text associated with an XElement object (for XElement objects with no children)

using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; public class LINQtoXML { public static void Main(string[] args) { XDocument doc = XDocument.Load(“StudentInfo.xml”); PrintElement(doc.Root, 0); } private static void PrintElement(XElement element, int indentLevel) { string name = element.Name.LocalName; Indent(indentLevel); Console.WriteLine(' '); if (element.HasElements) foreach (var child in element.Elements()) PrintElement(child,indentLevel+1); else { Indent(indentLevel+1); Console.WriteLine(element.Value.Trim()); } Indent(indentLevel); Console.WriteLine(" '); } private static void Indent(int level) { for (int i = 0; i < level; i++) Console.Write(" "); }

LINQ to XML

We can use LINQ to query an XML file in order to select specific elements or properties of elements We can use LINQ to query an XML file in order to select specific elements or properties of elements For example we can extract an element and all it’s descendants using the Descendants() method For example we can extract an element and all it’s descendants using the Descendants() method  We can then iterate through each of these using the Elements() method

LINQ to XML public class LINQtoXML { public static void Main(string[] args) { XDocument doc = XDocument.Load("StudentInfo.xml"); // Select a specific element var elements = from el in doc.Descendants("courseInfo") select el; foreach (var e in elements) PrintElement(e, 0); Console.WriteLine(); var elements1 = from e2 in doc.Descendants("courseInfo").Elements() where e2.Name.LocalName.StartsWith("c") select e2; foreach (var e in elements1) PrintElement(e, 0); } private static void PrintElement(XElement element, int indentLevel) {....} }

LINQ to XML

Summary We have looked at querying simple arrays using LINQ We have looked at querying simple arrays using LINQ We have looked at extension methods and how they are incorporated into LINQ We have looked at extension methods and how they are incorporated into LINQ We have looked at generic lists and how we query them using LINQ We have looked at generic lists and how we query them using LINQ We have looked at LINQ to XML and how we can query XML files using LINQ We have looked at LINQ to XML and how we can query XML files using LINQ