200 likes | 470 Views
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};
E N D
LINQ and Collections An introduction to LINQ and Collections
LINQ Language Integrated Query
LINQ canbeused 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 (varvaluein filtered) { Console.Write(" {0}", value); }
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
LINQ with objects varresult = from student in students wherestudent.LastName == "Vang" && student.FirstName != "Ebbe" selectstudent.FirstName; foreach (varfirstNamein result) { Console.WriteLine(firstName); }
LINQ exercise 1 • Create a linq statement thatretrieve all words from an array thatbegins or ends with an ‘e’
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); }
An anonymous type varresult = from student in students wherestudent.LastName == "Vang" && student.FirstName != "Ebbe" selectnew{ student.FirstName, student.LastName };
LINQ Exercise 2: • Write a LINQ statement thatselects all students from the programmingcourse
Exercise 2: answer varprogrammingStudents = from student in students wherestudent.Course == "Programming" select student; foreach (varprogStudinprogrammingStudents) { Console.WriteLine("{0}{1}", progStud.FirstName, progStud.LastName); }
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);
LINQ Exercise 3 • Write the twopreviousexercises as lambdaexpressions
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"));
Interface IEnumerable<T> • Wherecan i use LINQ? • Everywhereyoucanuseforeach (almost…)
ImplementingIEnummerable<> LINQ and lambdaexpressions for yourcatalogs
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()… }
Useyourcatalog… StudentCatalogstudentCatalog = newStudentCatalog(); varmyStud = studentCatalog.Where(s => s.LastName == "Vang"); foreach (var student inmyStud) { Console.WriteLine(student.FirstName); }