Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 77 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
77
Dung lượng
0,97 MB
Nội dung
Week 4: LINQ
• Basic concepts
– LINQ requirements
– Concepts
• Types
– LINQ to objects
– LINQ to SQL
– LINQ to Entity
– LINQ to XML
2. New features in langauge
• Generics
• Implicitly Typed Variables
• Object Initializers
• Anonymous Types
• Extension Methods
• Lambda Expressions
2.0 Generics
• the creation of various types of
collection
• type safety
• Binary Code Reuse
– using System.Collections.Generic;
2.1 Implicitly Typed Variables
• Implicitly Typed Variables
– Declare variables without specifying their type
– Strongly type, not variant, not object
– VS will determinr type
• Predict what the compiler will choose
• Intellisense support
– Type inference -> most general
• “3/10/2010” -> string, not date
2.1 Implicitly Typed Variables
• Example
var x = new OledbConnection();
2.1 Implicitly Typed Variables
• Note
– Alwayls declare the type if we know
– Implicitly Typed Variables are suited for LINQ and
anonymous type
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
• Usefull in any context
– Especially userfull in LINQ expressions
2.2 Object Initializers
• Example
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
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
[...]... namespaces • can be extended to support other data domains 3 What is LINQ: Everywhere • LINQ to Objects, LINQ to SQL, and LINQ to XML • LINQ is an extendable technology – open-source product: LINQExtender • build your own LINQ extensions • Other providers 3 What is LINQ: Everywhere 3 LINQ • new features of.NET framework support for LINQ: – Extension methods, lambda expressions • are used extensively... syntax used in LINQ is called a query expression • provides a set of standard query operators • common tasks: query, ordered, grouped oraggregated from an existing in-memory collection, a database, an XML file or many other sources 1 What is LINQ • How LINQ works? – compiler generates code • More detail: later 1 What is LINQ: Language Integration • Language in query expression 1 What is LINQ: LINQ Flavors... example 2.4.a delegate: turn back to example: lambda 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 3 LINQ example 1 3 LINQ requirements • Net framework 3.5 • VS 2008 • Work with VS 2005: not recommended – C# – VB.NET 3 What is LINQ • a programming model that introduces queries... extensively to build queries – anonymous types : to allow data to be returned from a query without the need to first define a class or structure, – generics : creation of various types of collection 3 LINQ basic • 2 types of Syntax – Query syntax – Method syntax . Week 4: LINQ
• Basic concepts
– LINQ requirements
– Concepts
• Types
– LINQ to objects
– LINQ to SQL
– LINQ to Entity
– LINQ to XML
2. New features. Alwayls declare the type if we know
– Implicitly Typed Variables are suited for LINQ and
anonymous type
2.2 Object Initializers
• Constructor
• Allow to assign