Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 93 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
93
Dung lượng
4,1 MB
Nội dung
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
[...]... delegate: turn back to example: lambda private void btnEven_Click(object sender, EventArgs e) { listNumber.SelectItemInListBox(x => x % 2 == 0); } HO CHI MINH UNIVERSITY OF INDUSTRY 3 LINQ Language Integrated Query LINQ does create a new way of looking at enumerations of data, and gives out-of-the-box functionality for them – L2O – L2S – L2E – L2X – L2D HO CHI MINH UNIVERSITY OF INDUSTRY 3 LINQ example . HO CHI MINH UNIVERSITY OF INDUSTRY
LINQ
Language
Integrated
Query
HO CHI MINH UNIVERSITY OF INDUSTRY
Basic concepts
LINQ requirements
Concepts
Types
LINQ. concepts & Types LinQ
HO CHI MINH UNIVERSITY OF INDUSTRY
2. New features in language:
Generics
Implicitly Typed Variables
Object Initializers
Anonymous