Linq
Linq
LINQ IN C#
OBJECTIVES
🢝 Become familiar with the basic concept of LINQ
🢝 Learn how to use LINQ to query an array
🢝 Learn how to sort an array using LINQ
🢝 Learn how to manipulate collections (e.g., List) by LINQ
2
LINQ
Language Integrated Query (LINQ)
Old way:
🢝 Structured Query Language (SQL)
🢝 Queries are written in string and passed to database which interprets the string,
processes the request, and returns the results
Data sources
🢝 Arrays
🢝 Collections
🢝 Files
4
WRITING AN LINQ QUERY
A LINQ query begins with a from clause
🢝 After from clause, specify a range variable and the data source to query
🢝 The range variable represents each item in the data source (much like foreach)
🢝 E.g., passed = from score in scores
The where clause gives a condition for the search and evaluates to
true or false
🢝 If true, the value is selected
// LINQ query to filter people older than 25 and project their names
QUERYING AN ARRAY
USING LINQ
var result = from person in people
where person.Age > 25
select person.Name;
COMPARE ARRAYS WITH
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
🢝 Efficient
10
LISTS
Dynamic resizing
LINQ can be used with different types of data sources, including lists
and arrays
🢝 SQL is only used with databases
11
COLLECTIONS
Collections do not 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.Collections.Generic
🢝 List<int> numbers = new List<int>();
🢝 numbers.Add(3);
12
EXAMPLE OF LIST
CODING
List<int> scores = new List<int>();
13
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
14
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");
15
STRUCT – EXAMPLE
public struct Info
{
public string fName;
public string lName;
public string Address1;
public string Address2;
public int zip;
public string Tele;
}
16
LIST OF STRUCT
public List <Info> friendsList = new List<Info>();
Info onePerson;
onePerson.fName=txt_fName.Text;
onePerson.lName=txt_lName.Text;
onePerson.Address1=txtAddr1.Text;
onePerson.Address2=txtAddr2.Text;
onePerson.zip=Convert.ToInt32 (txtZip.Text);
onePerson.Tele=txtTele.Text;
friendsList.Add(onePerson);
17
LINQ SEARCH FOR A LAST
NAME IN A LIST
//will select every person with that last name
18
DISPLAY THE SELECTED NAMES
AND TELEPHONE NUMBERS
foreach (var person in foundTele)
{
display +=person.fName+"
"+person.lName+"\t";
display += person.Tele + "\n";
}
var foundGPA =
from person in friendsList
where
person.GPA>=Convert.ToSingle(txtSearchGPA.Text)
select person;
20
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:")
21
MORE ABOUT FILE
INPUT/OUTPUT
…
22
OBTAINING FILE NAME
FROM CHOOSER
23
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();
24
WRITING TO THE FILE
StreamWriter outfile = new StreamWriter(fileName);
outfile.WriteLine(person.fName+","+person.lName+"
,"+person.Tele+","+Convert.ToString(person.GPA));
outfile.Close();
25
EXAMPLE OF EMPLOYEES
public class Employee
{
public string FirstName;
public string LastName;
public decimal monthlySalaryValue;
}
26
EXAMPLE OF EMPLOYEES
(CONT'D)
public class LINQwithArray
{
Employee [] employee_array;
LINQwithArray()
// … initializing arrays
Console.WriteLine("not found\n");
27