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
– 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
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
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
2.4 Extension Methods
• Example
2.4 Extension Methods
• Example
2.4.a Delegate
• Delegate
– refers to method.
– When initialize a delegate, we initialize it with
method.
2.4.a delegate: example
2.4.a delegate: why
2.4.a delegate: turn back to
example: lambda