LINQ and Collections An introduction to LINQ and Collections.

Slides:



Advertisements
Similar presentations
The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd
Advertisements

Stacks, Queues, and Linked Lists
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
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
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
.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#
C# and LINQ Yuan Yu Microsoft Research Silicon Valley.
Introduction to C# Properties, Arrays, Loops, Lists Game Design Experience Professor Jim Whitehead January 28, 2008 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
C# 3.0 Tom Roeder CS fa. Version 3 From PDC 2005 preview compiler available LINQ: language-integrated query High level points: adds native query.
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.
C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College
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.
LINQ Programming in C# LINQ CSE Prof. Roger Crawfis.
 Introduction  What is LINQ  Syntax  How to Query  Example Program.
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 11 th Lecture Pavel Ježek
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Based on material from Telerik Corporation.
Introduction to LINQ Lecture # 19 August Introduction How do you interrogate/manipulate data? What if you could do the work in a type-safe," string-free.
Neal Stublen How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,
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.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
Murach’s Visual Basic 2008, modified, C8© 2008, Mike Murach & Associates, Inc. Slide 1.
 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.
C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College
Arrays, Lists, Stacks, Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Arrays and Collections Tonga Institute of Higher Education.
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.
Pragmatic Mash-Ups Andrew Filev Microsoft Certified Architect Murano Software Building POC of Web 2.0 application in one day with AJAX, Virtual Earth and.
C#: Future Directions in Language Innovation Anders Hejlsberg TLN307 Technical Fellow Microsoft Corporation.
Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Advanced C#, part I Niels Hallenberg IT University of Copenhagen BAAAP – Spring 2009.
Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
CIS 200 Test 01 Review. Built-In Types Properties  Exposed “Variables” or accessible values of an object  Can have access controlled via scope modifiers.
Maps Nick Mouriski.
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
LINQ Language Integrated Query LINQ1. LINQ: Why and what? Problem Many data sources: Relational databases, XML, in-memory data structures, objects, etc.
LINQ and Lambda Expressions Telerik Software Academy LINQ Overview.
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.
Functional Programming Data Aggregation and Nested Queries Ivan Yonkov Technical Trainer Software University
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Introduction to.NET Florin Olariu “Alexandru Ioan Cuza”, University of Iai Department of Computer Science.
Computing with C# and the .NET Framework
Introduction to LINQ and Generic Collections
Visual Basic 2010 How to Program
MIS Professor Sandvig MIS 324 Professor Sandvig
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
Introduction to LINQ Chapter 11 10/28/2015 Lect 4 CT1411.
Introduction to LINQ Chapter 11.
STL - Algorithms.
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.
CSCI 3328 Object Oriented Programming in C# Review: Exam II
MIS Professor Sandvig MIS 324 Professor Sandvig
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.
CS4540 Special Topics in Web Development LINQ to Objects
Presentation transcript:

LINQ and Collections An introduction to LINQ and Collections

LINQ Language Integrated Query

LINQ can be used in many situations: Databases (Dlinq) XML (Xlinq) Collections

A basic LINQ example int[] values = {2, 9, 5, 0, 3, 7, 4, 8, 5, 4, 8, 9}; Console.WriteLine("original Array:"); foreach (var value in values) { Console.Write(" {0}", value); } var filtered = from value in values where value > 4 select value; Console.WriteLine("filtered (n>4):"); foreach (var value in filtered) { Console.Write(" {0}", value); }

LINQ var filtered = from value in values where value > 4 orderby value descending select value; from value in values defines a variavble name: value var filtered Implicitly typed where value > 4 adds item when true orderby descending sorting mechanism select value what is selected

LINQ with objects var result = from student in students where student.LastName == "Vang" && student.FirstName != "Ebbe" select student.FirstName; foreach (var firstName in result) { Console.WriteLine(firstName); }

LINQ exercise 1 Create a linq statement that retrieve all words from an array that begins or ends with an ‘e’

Exercise 1 answer String[] names = {"Allan", "Ebbe", "Eric", "Liv", "Rick","Michael"}; var listNames = from name in names where name.EndsWith("e") || name.StartsWith("E") select name; foreach (var name in listNames) { Console.WriteLine(name); }

An anonymous type var result = from student in students where student.LastName == "Vang" && student.FirstName != "Ebbe" select new{ student.FirstName, student.LastName };

LINQ Exercise 2: Write a LINQ statement that selects all students from the programming course

Exercise 2: answer var programmingStudents = from student in students where student.Course == "Programming" select student; foreach (var progStud in programmingStudents) { Console.WriteLine("{0} {1}", progStud.FirstName, progStud.LastName); }

Lambda Expressions var result = from student in students where student.LastName == "Vang" && student.FirstName != "Ebbe" select student.FirstName; VS var result = students.Where(s => s.LastName == "Vang" && s.FirstName != "Ebbe").Select(s => s.FirstName);

LINQ Exercise 3 Write the two previous exercises as lambda expressions

Exercise 3 answer var programmingStudents = from student in students where student.Course == "Programming" select student; var programmingStudents = students.Where(s => s.Course == "Programming"); var listNames = from name in names where name.EndsWith("e") || name.StartsWith("E") select name; var listNames = names.Where(n => n.EndsWith("e") || n.StartsWith("E"));

Interface IEnumerable Where can i use LINQ? Everywhere you can use foreach (almost…)

Collections (C#)

System.Collections Classes Dictionary Represents a collection of key/value pairs that are organized based on the key. List Represents a list of objects that can be accessed by index. Provides methods to search, sort, and modify lists. Queue Represents a first in, first out (FIFO) collection of objects. SortedList Represents a collection of key/value pairs that are sorted by key based on the associated IComparer implementation.IComparer Stack Represents a last in, first out (LIFO) collection of objects.

Implementing IEnummerable<> LINQ and lambda expressions for your catalogs

Implement the interface class StudentCatalog : IEnumerable { private List _students; public StudentCatalog()… IEnumerator IEnumerable.GetEnumerator() { foreach (Student s in _students) { // Lets check for end of list (its bad code since we used arrays) if (s == null) { break; } // Return the current element and then on next function call // resume from next element rather than starting all over again; yield return s; } } public IEnumerator GetEnumerator()… }

Use your catalog… StudentCatalog studentCatalog = new StudentCatalog(); var myStud = studentCatalog.Where(s => s.LastName == "Vang"); foreach (var student in myStud) { Console.WriteLine(student.FirstName); }