Download presentation
Presentation is loading. Please wait.
1
C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College pavely@hi-tech.co.il http://blogs.microsoft.co.il/blogs/pavely
2
Agenda C# 3.0 New Features Introduction to LINQ LINQ to Objects LINQ to SQL LINQ to XML Summary 2©2008 Pavel Yosifovich
3
C# 3.0 Features Implicitly Typed Local Variables Automatic Properties Object and Collection Initializers Anonymous Types Extension Methods Lambda Expressions LINQ 3©2008 Pavel Yosifovich
4
Implicitly Typed Local Variables The var keyword // C# 2.0 int x = 5; string name = "Bart Simpson"; Dictionary data = new Dictionary (); int size = name.Length; // C# 2.0 int x = 5; string name = "Bart Simpson"; Dictionary data = new Dictionary (); int size = name.Length; // C# 3.0 var x = 5; var name = "Bart Simpson"; var data = new Dictionary (); var size = name.Length; var y = x; var keys = data.Keys;// Dictionary.KeyCollection // C# 3.0 var x = 5; var name = "Bart Simpson"; var data = new Dictionary (); var size = name.Length; var y = x; var keys = data.Keys;// Dictionary.KeyCollection 4©2008 Pavel Yosifovich
5
Automatic Properties public class Person {// C# 2.0 private string _firstName, _lastName; private int _age; public string FirstName { get { return _firstName; } set { _firstName = value; } } public string LastName { get { return _lastName; } set { _lastName = value; } } public int Age { get { return _age; } set { _age = value; } } public class Person {// C# 2.0 private string _firstName, _lastName; private int _age; public string FirstName { get { return _firstName; } set { _firstName = value; } } public string LastName { get { return _lastName; } set { _lastName = value; } } public int Age { get { return _age; } set { _age = value; } } public class Person {// C# 3.0 public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } } public class Person {// C# 3.0 public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } } 5©2008 Pavel Yosifovich
6
Object Initializers // C# 2.0 Person p = new Person(); p.FirstName = "Bart"; p.LastName = "Simpson"; p.Age = 12; // C# 2.0 Person p = new Person(); p.FirstName = "Bart"; p.LastName = "Simpson"; p.Age = 12; // C# 3.0 Person p = new Person() { FirstName = "Bart", LastName = "Simpson", Age = 12 }; // C# 3.0 Person p = new Person() { FirstName = "Bart", LastName = "Simpson", Age = 12 }; 6©2008 Pavel Yosifovich
7
Collection Initializers // C# 3.0 List people = new List (); people.Add(new Person() { FirstName = "Bart", LastName = "Simpson", Age = 12 }); people.Add(new Person() { FirstName = "Clark", LastName = "Kent", Age = 35 }); people.Add(new Person() { FirstName = "Peter", LastName = "Parker", Age = 30 }); // C# 3.0 List people = new List (); people.Add(new Person() { FirstName = "Bart", LastName = "Simpson", Age = 12 }); people.Add(new Person() { FirstName = "Clark", LastName = "Kent", Age = 35 }); people.Add(new Person() { FirstName = "Peter", LastName = "Parker", Age = 30 }); // C# 3.0 var people = new List () { new Person() { FirstName = "Bart", LastName = "Simpson", Age = 12 }, new Person() { FirstName = "Clark", LastName = "Kent", Age = 35 }, new Person() { FirstName = "Peter", LastName = "Parker", Age = 30 } }; // C# 3.0 var people = new List () { new Person() { FirstName = "Bart", LastName = "Simpson", Age = 12 }, new Person() { FirstName = "Clark", LastName = "Kent", Age = 35 }, new Person() { FirstName = "Peter", LastName = "Parker", Age = 30 } }; 7©2008 Pavel Yosifovich
8
Anonymous Types var people = new[] { new { FirstName = "Clark", LastName = "Kent", Age = 36 }, new { FirstName = "Peter", LastName = "parker", Age = 26 }, new { FirstName = "Bart", LastName = "Simpson", Age = 11 } }; foreach (var i in people) Console.WriteLine("{0} {1} ({2})", i.FirstName, i.LastName, i.Age); Console.WriteLine(people[0].GetType().FullName);// ??? var people = new[] { new { FirstName = "Clark", LastName = "Kent", Age = 36 }, new { FirstName = "Peter", LastName = "parker", Age = 26 }, new { FirstName = "Bart", LastName = "Simpson", Age = 11 } }; foreach (var i in people) Console.WriteLine("{0} {1} ({2})", i.FirstName, i.LastName, i.Age); Console.WriteLine(people[0].GetType().FullName);// ??? 8©2008 Pavel Yosifovich
9
Extension Methods public static class MyExtensions { public static string UpperLower(this string str, bool upperFirst) { StringBuilder newString = new StringBuilder(str.Length); for (int i = 0; i < str.Length; i++) { newString.Append(upperFirst ? char.ToUpper(str[i]) : char.ToLower(str[i])); upperFirst = !upperFirst; } return newString.ToString(); } public static class MyExtensions { public static string UpperLower(this string str, bool upperFirst) { StringBuilder newString = new StringBuilder(str.Length); for (int i = 0; i < str.Length; i++) { newString.Append(upperFirst ? char.ToUpper(str[i]) : char.ToLower(str[i])); upperFirst = !upperFirst; } return newString.ToString(); } string input = Console.ReadLine(); Console.WriteLine("calling extension method for {0}: {1}", input, input.UpperLower(true)); string input = Console.ReadLine(); Console.WriteLine("calling extension method for {0}: {1}", input, input.UpperLower(true)); 9©2008 Pavel Yosifovich
10
EXTENSION METHODS DEMO
11
Problem Strongly typed, Intellisense, Compilers, Debuggers (Imperative) Projection, Join, Grouping, Queries (Declarative) Data as Objects Objects as Data 11©2008 Pavel Yosifovich
12
What is LINQ? Unified programming model for any data type/source –Collections –Database Relational Data –XML Files –Extendable for anything else Introduce more declarative syntax Data is equivalent to Objects 12©2008 Pavel Yosifovich
13
LINQ enabled ADO.NET LINQ Architecture XML Objects Relational Data.NET Language-Integrated Query (LINQ) 13©2008 Pavel Yosifovich
14
LINQ Syntax Fundamentals Syntax based on Extension methods Some Extension methods may be replaced by language keywords –where, orderby, select, group, … Auxiliary language features in use –Automatic properties –Anonymous types –Implicitly typed local variables –Object initializers 14©2008 Pavel Yosifovich
15
LINQ To Objects Working with collections –Any one that implements IEnumerable using System.Linq System.Core.Dll assembly Deferred Execution
16
LINQ TO OBJECTS DEMO
17
Classic ADO.NET SqlConnection conn = new SqlConnection(“...“); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = @“ SELECT * FROM Vehicles WHERE Model = @Model"; cmd.Parameters.Add("@Model", “Mazda 3“); SqlDataReader r = cmd.ExecuteReader(); while ( r.HasRows ) { Console.WriteLine(r[“Number"] + r[“Year"]); } SqlConnection conn = new SqlConnection(“...“); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = @“ SELECT * FROM Vehicles WHERE Model = @Model"; cmd.Parameters.Add("@Model", “Mazda 3“); SqlDataReader r = cmd.ExecuteReader(); while ( r.HasRows ) { Console.WriteLine(r[“Number"] + r[“Year"]); } Application Relational Database No intellisence No compile time checks Loosely bound arguments Untyped Results
18
LINQ To SQL The DataContext type Custom attributes ( Table, Column ) Not just “Query” Can use stored procedures using System.Data.Linq System.Data.Linq.Dll Assembly
19
LINQ TO SQL DEMO
20
LINQ To SQL Performance Performance is good –97% of classic ADO.NET Optimizations –Turn track checking off for reading only ObjectTrackingEnabled = false –Use the DataLoadOptions type to minimize round trips LoadWith<>, AssociateWith<> instance methods –Compile frequently used queries CompiledQuery.Compile (…)
21
Handling Concurrency Optimistic concurrency mode IsVersion property on Column attribute UpdateCheck property – Always (default) – Never – WhenChanged SubmitChanges argument – FailOnConflict (default) – ContinueOnColflict DataContext has ChangedConflicts ©2008 Pavel Yosifovich21
22
LINQ To XML New object model –No need to create a document –Very intuitive and flexible using System.Xml.Linq System.Xml.Linq.Dll Assembly Easy to combine with other LINQ providers –E.g. LINQ to SQL
23
LINQ TO XML DEMO
24
©2008 Pavel Yosifovich24 Q & A ?
25
Summary LINQ Allows using data as objects and vice versa Same syntax across any provider C# Language support Use it today!
26
Agenda 1. Agenda Item 2. Agenda Item 3. Agenda Item 4. Agenda Item 26©2008 Pavel Yosifovich
27
Cycle Diagram Text Cycle name Add Your Text 27©2008 Pavel Yosifovich
28
Progress Diagram Phase 1 Phase 2 Phase 3 28©2008 Pavel Yosifovich
29
Block Diagram TEXT 29©2008 Pavel Yosifovich
30
Table TEXT Title A Title B Title C Title D Title E Title F 30©2008 Pavel Yosifovich
31
3-D Pie Chart TEXT 31©2008 Pavel Yosifovich
32
Marketing Diagram Title TEXT 32©2008 Pavel Yosifovich
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.