Post on 10-May-2015
description
Developing Windows and Web Applications using Visual Studio.NET
Peter Gfader
Who did the C# tutorial?
What is .NET?
What is the .NET framework?
What is the CLR?
What is OOP?
Session 1: Last week
Agenda
C# 3.0 Language Enhancement
C# 4.0 Language Enhancement
C# 5
What is LINQ?
LINQ Project Goals
LINQ Project Architecture
Data Binding via LINQ
Session 2: Overview
C# 32008
Local Variable Type Interface
Anonymous Type
Lambda Expressions
Extension methods
Object initializers
Query Expressions
C# 3.0 Language Enhancement
Local Variable Type Inference
Fits with complex temporarily variables
E.g. I want a temp variable to hold username & password
Anonymous Type
Anonymous methods in C#2
NumericalOperation mul1 = (x, y) => x * y;
Console.WriteLine( mul1(4, 5) );
Lambda Expressions
Extend every class with a static „helper“ method
Extension methods
More
Object initializer’s
Query Expressions
Please visit:
HookedOnLINQ.com
C# 4April 2010
„dynamic“ keyword
Optional parameters
Named parameters
Co and Contra variance
C# 4 Language Enhancement
Co and Contra varianceHow are generic types “broken” today?
var sheep = new List<Sheep>(); Speak(sheep);
void Speak(IEnumerable<Animal> animals) { // Do something with Animals }
class Animal { } class Sheep : Animal { }
Not Allowed:IEnumerable<Animal> != IEnumerable<Sheep>
Covariance – think “out”
Contravariance – think “in”
Break it down…
C# 5????? 2013 ????
„Meta programming is painful”
Compiler as a service
C# 5 ? - Release date?
Auto Implemented Properties
Collection Initializers
Named parameters
Optional parameters
Lambdas
Can span statements across multiple lines
Auto Implemented Properties
Collection Initializers (limited)
Named parameters
Optional parameters
Lambdas (limited)
Can span statements across multiple lines
.NET 3.5
Auto Implemented Properties
Collection Initializers
Named parameters
Optional parameters
Lambdas
Can span statements across multiple lines
Auto Implemented Properties
Collection Initializers
Named parameters
Optional parameters
Lambdas
Can span statements across multiple lines
.NET 4
Data accessShow me the data!
The problem
Problem
First showcased at the American International Toy Fair in New York in 2005
LINQ, a clever bluffing game
What is LINQ?
Language INtegrated Query
Allows you to easily query information about an object
LINQ to SQL: An ORM made by Microsoft LINQ to Entities: Another ORM made by Microsoft LINQ to XML: Query over XML ...
What is LINQ?
Language syntax+ a library of extension methods+ lambda expressions+ anonymous types+ a little magic
What is LINQ?
.NET Framework 3.5
LINQ is a natural language extension, part of .NET 3.5
Different data sources are queried with the same syntax.
Queries are strongly typed
Queries benefit from “Intellisense”
Applications becomes less error prone
LINQ - How does it solve the problem?
Unified programming model for any data type
Database Relational Data XML Files Collections & Arrays
Introduce more declarative syntax
Helps the system find the best execution strategy
LINQ Project Goals
LINQ Enabled Data Sources
LINQ Enabled ADO.NET
C# VB Others...
.NET Language Integrated Query (LINQ)
LINQ to
Objects
LINQ to Dataset
s
LINQ to
SQL
LINQ to
Entities
LINQ to
XML
LINQ to ??
?
<book> <title>
<author/> <price
/> </title></book>
Objects Database XML
LINQ Project Architecture
LINQ Query
string strSQL = “SELECT custID FROM Customers“;
SqlCommand cmd = new SqlCommand(strSQL, cnn);
SqlDataAdapter adAdapter = new SqlDataAdapter(cmd);
DataSet dst = new DataSet();
adAdapter.Fill(dst);
Standard Data Access Code
NorthwindDataContext db = new NorthwindDataContext();
var customers = from c in db.Customers
select c;
Code by LINQ
How to use LINQ
Benefits from LINQ
LINQ data binding
LINQ
Stored procedures are easier to work with
Relationship editor UI is nicer (more like SQL Server)
Performance
In a recent benchmark LINQ to SQL performed 5 times faster than LINQ to Entities
LINQ 2 SQL
More refined ORM than LINQ to SQL
Many to Many relationships
Richer support for Inheritance
Validation
Update model from database
Mapping Editor
LINQ 2 Entities
Do you know why you should choose "LINQ to Entities" over "LINQ to SQL"?
http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterLINQ.aspx#WhyLINQtoEntitiesNotSQL
What to choose?
New features in C# 3.0
New features in C# 4.0
What is LINQ and Why LINQ
LINQ data binding
Summary
Introduction to LINQ http://msdn.microsoft.com/en-us/library/bb397897.aspx
Getting Started with LINQ in C# http://msdn.microsoft.com/en-us/library/bb397933.aspx
LINQ Term and more details http://www.hookedonlinq.com/
LinqPad http://www.linqpad.net/
Resources
What's next?
Thank You!
Gateway Court Suite 10 81 - 91 Military Road Neutral Bay, Sydney NSW 2089 AUSTRALIA
ABN: 21 069 371 900
Phone: + 61 2 9953 3000 Fax: + 61 2 9953 3105
info@ssw.com.auwww.ssw.com.au