1 / 21

LINQ AND GENERIC COLLECTIONS

LINQ AND GENERIC COLLECTIONS. DR. JOHN P. ABRAHAM PROFESSOR UTPA. COMPARE ARRAYS TO COLLECTIONS. You must specify an arrays size You can resize it in some languages at runtime .NET framework generic collections gives greater flexibility, reusable, reliable, powerful and efficient. Lists.

akasma
Télécharger la présentation

LINQ AND GENERIC 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 GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

  2. COMPARE ARRAYS TO COLLECTIONS • You must specify an arrays size • You can resize it in some languages at runtime • .NET framework generic collections gives greater flexibility, reusable, reliable, powerful and efficient.

  3. Lists • Dynamic resizing • SQL was only used with databases, now LINQ can be used with different types of data sources, including lists and arrays

  4. Collections • Collections don’t have a fixed size • Size of a collection is increased automatically when elements are added to it. • Collections: lists, sorted lists, queues, stacks and array lists. • UnTyped and typed collections. • Using System.Collections • ArrayList numbers = new ArrayList(); • numbers.Add(3); • Using System.Colelctions.Generic • List<int> numbers = new List<int>(); • numbrs.Add(3);

  5. Example List coding string[] grades = txtEnterGrade.Text.Split(' '); List<int> scores = new List<int>(); int numScores = grades.GetUpperBound(0)+1; foreach (string grade in grades) { scores.Add(Convert.ToInt32(grade)); lstBoxGrades.Items.Add(grade); }

  6. Example continued public double stdandardDeviation(List<int> scores, int numScores)//declaration {int sum=0; foreach (int score in scores) { sum = sum + score; } }

  7. Stack Operation (built-in) Stack<string> students = new Stack<string>(); students.Push("Abbit"); students.Push("Aguero"); students.Push("Castro"); students.Push("Chen"); students.Push("Cruz"); while (students.Count > 0) MessageBox.Show("Popped: " + students.Pop());

  8. Some methods/properties of list .Add -adds to the end of the list .Insert -inserts an element at the specified index .Clear –removes all elements from the list .Remove –removes the first occurrence of the specified value. .Contains – returns true if the List contains value .Sort .

  9. LINQ • Language Integrated Query • LINQ libraries are known as providers • A LINQ provider is a set of classes that implement LINQ operations. • FLAVORS OF LINQ • LINQ TO SQL • LINQ TO XML • LINQ TO OBJECTS

  10. Querying an Array using LINQ • To search for scores >=60 var passed = from score in scores where score >= 60 select score; foreach (var score in passed) { lstLINQresult.Items.Add(score); }

  11. Show Program Example Standard deviation extended.

  12. LINQ • Must included System.LINQ name space • Query begins with a from clause • Then assign a temporary variable like score and the data source like scores. passed = from score in scores The search results will be saved in passed. The where clause gives a condition for the search The select clause determine what will be saved in passed.

  13. Struct – Example public struct Info { public string Name; public string Address1; public string Address2; public Int32 zip; public string Tele; }

  14. List of Struct public List <Info> friendsList = new List<Info>(); Info onePerson; onePerson.Name=txtName.Text; onePerson.Name=txtName.Text; onePerson.Address1=txtAddr1.Text; onePerson.Address2=txtAddr2.Text; onePerson.zip=Convert.ToInt32 (txtZip.Text); onePerson.Tele=txtTele.Text; friendsList.Add(onePerson);

  15. Obtaining file name from chooser OpenFileDialog fDialog = new OpenFileDialog(); if (fDialog.ShowDialog() == DialogResult.OK) { fileName = (fDialog.FileName.ToString()); MessageBox.Show(fileName); }

  16. Reading from a file • TextReader ofile = new StreamReader(fileName); while (ofile.Peek()!=-1) { string oneline = ofile.ReadLine(); MessageBox.Show(oneline,"Reading From File.."); string[] items = oneline.Split(','); onePerson.fName = items[0]; onePerson.lName = items[1]; onePerson.GPA = Convert.ToSingle(items[3]); onePerson.Tele = items[2]; friendsList.Add(onePerson); } ofile.Close();

  17. Writing to the file StreamWriter outfile = new StreamWriter(fileName); foreach (Info person in friendsList) outfile.WriteLine(person.fName+","+person.lName+","+person.Tele+","+Convert.ToString(person.GPA)); outfile.Close();

  18. LINQ Search for a Last Name var foundTele = from person in friendsList where person.lName == txtSearchName.Text select person; --will select every person with that last name—

  19. Display the selected names & tele foreach (var person in foundTele) { display += person.fName + " " + person.lName + "\t"; display += person.Tele + "\n"; } MessageBox.Show(display, "Names and Telephone Nos. Persons searched:");

  20. Select those with GPA >+ var foundGPA = from person in friendsList where person.GPA >= Convert.ToSingle(txtSearchGPA.Text) select person;

  21. Display foreach (var person in foundGPA) { display += person.fName + " " + person.lName + "\t"; display += person.Tele + "\t"; display += Convert.ToString(person.GPA)+"\n"; } MessageBox.Show(display, "Names and Telephone Nos. Persons searched:")

More Related