LINQ AND GENERIC COLLECTIONS

Preview:

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

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

• Dynamic resizing

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

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

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

}

Example continued

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

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

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

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

.

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

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

}

Show Program Example

Standard deviation extended.

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.

Struct – Example

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

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

Obtaining file name from chooser

OpenFileDialog fDialog = new OpenFileDialog();

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

{

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

MessageBox.Show(fileName);

}

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

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

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—

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

Select those with GPA >+

var foundGPA =

from person in friendsList

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

select person;

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

Recommended