21
LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

Embed Size (px)

Citation preview

Page 1: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

LINQ AND GENERIC COLLECTIONS

DR. JOHN P. ABRAHAM

PROFESSOR

UTPA

Page 2: 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.

Page 3: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

Lists

• Dynamic resizing

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

Page 4: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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

Page 5: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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

}

Page 6: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

Example continued

public double stdandardDeviation(List<int> scores, int numScores)//declaration

{int sum=0;foreach (int score in scores) { sum = sum + score; }}

Page 7: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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

Page 8: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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

.

Page 9: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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

Page 10: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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

}

Page 11: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

Show Program Example

Standard deviation extended.

Page 12: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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.

Page 13: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

Struct – Example

public struct Info { public string Name; public string Address1; public string Address2; public Int32 zip; public string Tele; }

Page 14: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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

Page 15: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

Obtaining file name from chooser

OpenFileDialog fDialog = new OpenFileDialog();

if (fDialog.ShowDialog() == DialogResult.OK)

{

fileName = (fDialog.FileName.ToString());

MessageBox.Show(fileName);

}

Page 16: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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

Page 17: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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

Page 18: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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—

Page 19: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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

Page 20: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

Select those with GPA >+

var foundGPA =

from person in friendsList

where person.GPA >= Convert.ToSingle(txtSearchGPA.Text)

select person;

Page 21: LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA

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:")