1 / 20

LINQ and Collections

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

lucine
Télécharger la présentation

LINQ and Collections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LINQ and Collections An introduction to LINQ and Collections

  2. LINQ Language Integrated Query

  3. LINQ canbeused 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 (varvaluein filtered) { Console.Write(" {0}", value); }

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

  6. LINQ with objects varresult = from student in students wherestudent.LastName == "Vang" && student.FirstName != "Ebbe" selectstudent.FirstName; foreach (varfirstNamein result) { Console.WriteLine(firstName); }

  7. LINQ exercise 1 • Create a linq statement thatretrieve all words from an array thatbegins or ends with an ‘e’

  8. Exercise 1 answer String[] names = {"Allan", "Ebbe", "Eric", "Liv", "Rick","Michael"}; varlistNames = fromnameinnames wherename.EndsWith("e") || name.StartsWith("E") selectname; foreach (varnameinlistNames) { Console.WriteLine(name); }

  9. An anonymous type varresult = from student in students wherestudent.LastName == "Vang" && student.FirstName != "Ebbe" selectnew{ student.FirstName, student.LastName };

  10. LINQ Exercise 2: • Write a LINQ statement thatselects all students from the programmingcourse

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

  12. Lambda Expressions varresult = from student in students wherestudent.LastName == "Vang" && student.FirstName != "Ebbe" selectstudent.FirstName; VS varresult = students.Where(s => s.LastName == "Vang" && s.FirstName != "Ebbe").Select(s => s.FirstName);

  13. LINQ Exercise 3 • Write the twopreviousexercises as lambdaexpressions

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

  15. Interface IEnumerable<T> • Wherecan i use LINQ? • Everywhereyoucanuseforeach (almost…)

  16. Collections (C#)

  17. System.CollectionsClasses

  18. ImplementingIEnummerable<> LINQ and lambdaexpressions for yourcatalogs

  19. Implement the interface classStudentCatalog : IEnumerable<Student>{privateList<Student> _students; publicStudentCatalog()… IEnumerator<Student> IEnumerable<Student>.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;yieldreturn s;}}publicIEnumeratorGetEnumerator()… }

  20. Useyourcatalog… StudentCatalogstudentCatalog = newStudentCatalog(); varmyStud = studentCatalog.Where(s => s.LastName == "Vang"); foreach (var student inmyStud) { Console.WriteLine(student.FirstName); }

More Related