HO CHI MINH UNIVERSITY OF INDUSTRY
LINQ
Language
Integrated
Query
HO CHI MINH UNIVERSITY OF INDUSTRY
Basic concepts
LINQ requirements
Concepts
Types
LINQ to objects
LINQ to SQL
LINQ to Entity
LINQ to XML
1. Basic concepts & Types LinQ
HO CHI MINH UNIVERSITY OF INDUSTRY
2. New features in language:
Generics
Implicitly Typed Variables
Object Initializers
Anonymous Types
Extension Methods
Lambda Expressions
HO CHI MINH UNIVERSITY OF INDUSTRY
2.0. Generics
The creation of various types of collection
Type safety
Binary Code Reuse
using System.Collections.Generic;
List<int> intS = new List<int>();
intS.Add(113);
intS.Add(114);
int nAt = intS[0];
HO CHI MINH UNIVERSITY OF INDUSTRY
2.1. Implicitly Typed Variables
Implicitly Typed Variables
– Declare variables without specifying their type
– Strongly type, not variant, not object
– Visual Studio will determine type
• Predict what the compiler will choose
• Intelligence support
– Type inference -> most general
• “3/10/2010” -> string, not date
HO CHI MINH UNIVERSITY OF INDUSTRY
Example
2.1. Implicitly Typed Variables
private void button2_Click(object sender, EventArgs e)
{ var x = 113;
var y = "1/1/2012";
var z = 1.7;
var k = new DateTime(2012, 1, 1);
string msg = "x type="+x.GetType() + "\n"+
"y type = "+y.GetType() + "\n" +
"z type ="+z.GetType() + "\n" +
"k type = " + k.GetType();
MessageBox.Show(msg);
}
HO CHI MINH UNIVERSITY OF INDUSTRY
2.1. Implicitly Typed Variables
Note
– Always declare the type if we know
– Implicitly Typed Variables are suited for LINQ and anonymous type
HO CHI MINH UNIVERSITY OF INDUSTRY
2.2. Object Initializers
Constructor
Allow to assign values to object properties (fields) when create object
We do not have to explicitly invoke a constructor
Useful in any context
– Especially useful in LINQ expressions
HO CHI MINH UNIVERSITY OF INDUSTRY
2.2. Object Initializers
Example
class Test
{
public int a, b;
public int a2 { set;get;}
public int b2 { get; set; }
}
private void button3_Click(object sender, EventArgs e)
{
var x = new Test() {a=113,b=114,a2=115,b2=116};
MessageBox.Show(x.a2+"");
}
HO CHI MINH UNIVERSITY OF INDUSTRY
2.3. Anonymous Types
Implicitly type functionality for objects
– Set property values to object without writing class definition
– The resulting class has no usable name
– Class name is generated by compiler, inherits from Object
– The result: an anonymous type that is not available at source code level
Also called Projections
HO CHI MINH UNIVERSITY OF INDUSTRY
2.3. Anonymous Types
When to user anonymous types
– Need a temporary object to hold related data
– Don’t need method
– If we need a different set of properties for each declaration
– If we need to change the order of properties for each declaration
HO CHI MINH UNIVERSITY OF INDUSTRY
2.3. Anonymous Types
When not to user anonymous types
– Need to define methods
– Need to define another variable
– Need to shared data across methods
private void btn4_Click(object sender, EventArgs e)
{
var teo = new { ID=1234,Name="Tèo Hả Tèo"};
MessageBox.Show(teo.ID +"-"+teo.Name);
}
HO CHI MINH UNIVERSITY OF INDUSTRY
2.4. Extension Methods
Special kind of Static method
Allow the addition of methods to an existing class
– Without creating a new derived type
– Without re-compiling or modifying the original type
Called The Same way regular methods are called
Define in static class
HO CHI MINH UNIVERSITY OF INDUSTRY
2.4. Extension Methods
Example
namespace StudyLinQ
{ public static class MyExtensionMethod
{
public static int SumFrom1toN(this int n)
{ int sum = 0;
for (int i = 1; i <= n; i++)
sum += i;
return sum;
}
}
}
HO CHI MINH UNIVERSITY OF INDUSTRY
2.4. Extension Methods
Example
MessageBox.Show(n.SumFrom1toN()+"");
HO CHI MINH UNIVERSITY OF INDUSTRY
namespace StudyLinQ
{
public static class MyExtensionMethod
{
public static int SumFrom1toN(this int n){…}
public static string NoiChuoi(this string s1, string s2)
{
return s1 + "-" + s2;
}
}
}
2.4. Extension Methods
Example
HO CHI MINH UNIVERSITY OF INDUSTRY
private void btnNoi_Click
(object sender, EventArgs e)
{
string s = "Tý";
MessageBox.Show( s.NoiChuoi("Mập"));
}
2.4. Extension Methods
Example
HO CHI MINH UNIVERSITY OF INDUSTRY
2.4. Extension Methods
Example
using System.Windows.Forms;
using System.Drawing;
namespace StudyLinQ
{
public static class MyExtensionMethod
{
public static int SumFrom1toN(this int n) {… }
public static string NoiChuoi(this string s1, string s2){…}
public static void ChangeColorToRed(this Button btn)
{
btn.BackColor = Color.Red;
}
}
}
HO CHI MINH UNIVERSITY OF INDUSTRY
2.4. Extension Methods
Example
btnColor.ChangeColorToRed();
HO CHI MINH UNIVERSITY OF INDUSTRY
2.4.a. Delegate
Delegate
– refers to method.
– When initialize a delegate, we initialize it with method.
Example
//Defines a delegate
public delegate int ChangeInt(int x);
public int Tang2(int x){return x + 2;}
public int Giam2(int x) {return x - 2;}
HO CHI MINH UNIVERSITY OF INDUSTRY
2.4.a. Delegate
Example
private void btndlg_Click
(object sender, EventArgs e)
{
ChangeInt d = new ChangeInt(Tang2);
MessageBox.Show(d(2)+"");
d = new ChangeInt(Giam2);
MessageBox.Show(d(2)+"");
}
HO CHI MINH UNIVERSITY OF INDUSTRY
2.4.a. delegate: why?
HO CHI MINH UNIVERSITY OF INDUSTRY
private void btnEven_Click(object sender, EventArgs e)
{
listNumber.SelectedIndex = -1;
for (int i = 0; i < listNumber.Items.Count; i++)
{
if (Convert.ToInt32(listNumber.Items[i]) % 2 == 0)
listNumber.SelectedIndex = i;
}
}
private void btnOdd_Click(object sender, EventArgs e)
{
listNumber.SelectedIndex = -1;
for (int i = 0; i < listNumber.Items.Count; i++)
{
if (Convert.ToInt32(listNumber.Items[i]) % 2 != 0)
listNumber.SelectedIndex = i;
}
}
HO CHI MINH UNIVERSITY OF INDUSTRY
private bool isPrime(int x)
{
if (x < 2)
return false;
for (int i = 2; i <= Math.Sqrt(x); i++)
if (x % i == 0)
return false;
return true;
}
private void btnPrime_Click(object sender, EventArgs e)
{
listNumber.SelectedIndex = -1;
for (int i = 0; i < listNumber.Items.Count; i++)
{
int x=Convert.ToInt32(listNumber.Items[i]);
if ( isPrime(x))
listNumber.SelectedIndex = i;
}
}
HO CHI MINH UNIVERSITY OF INDUSTRY
2.4.a. delegate: why?
public static class MyExtensionMethod
{
public delegate bool HandleFunction(int x);
public static void SelectItemInListBox(this ListBox lst,
HandleFunction fn)
{
lst.SelectedIndex = -1;
for (int i = 0; i < lst.Items.Count; i++)
{
int x=Convert.ToInt32(lst.Items[i]);
if (fn(x))
lst.SelectedIndex = i;
}
}
}