Presentation is loading. Please wait.

Presentation is loading. Please wait.

LINQ and Collections An introduction to LINQ and Collections.

Similar presentations


Presentation on theme: "LINQ and Collections An introduction to LINQ and Collections."— Presentation transcript:

1 LINQ and Collections An introduction to LINQ and Collections

2 LINQ Language Integrated Query

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

4 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); }

5 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

6 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); }

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

8 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); }

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

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

11 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); }

12 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);

13 LINQ Exercise 3 Write the two previous exercises as lambda expressions

14 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"));

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

16 Collections (C#)

17 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.

18 Implementing IEnummerable<> LINQ and lambda expressions for your catalogs

19 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()… }

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


Download ppt "LINQ and Collections An introduction to LINQ and Collections."

Similar presentations


Ads by Google