C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College

Slides:



Advertisements
Similar presentations
Developer Knowledge Sharing Eric Sun Dec, What programming language did you learn in school and since then? Now, its time to refresh …
Advertisements

The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd
Language Integrated Query (LINQ) Martin Parry Developer & Platform Group Microsoft Ltd
LINQ and Collections An introduction to LINQ and Collections.
Michael Pizzo Software Architect Data Programmability Microsoft Corporation.
ASP.NET Best Practices Dawit Wubshet Park University.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
.NET 3.5 – Mysteries. NetFx Evolution NetFx 1.0 C# 1.0, VB 7.0, VS.NET NetFx 1.1 C# 1.1, VB 7.1, VS 2003 NetFx 2.0 C# 2.0, VB 8.0, VS 2005 NetFx 3.0 C#
© Logica All rights reserved ADO vNext LINQ LINQ to SQL Entity Framework Freek Leemhuis
ASP.NET 3.5 New Features. 2 Agenda What's New in.NET Framework 3.5? Visual Studio 2008 Enhancements LINQ (Language Integrated Query) New ASP.NET Server.
LinqToSharePoint SandBoxed Solution Shakir Majeed Khan
C# 3.0 Tom Roeder CS fa. Version 3 From PDC 2005 preview compiler available LINQ: language-integrated query High level points: adds native query.
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Microsoft Entity Framework v1.1 over Oracle Database Erez.
2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.
C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College
A tour of new features introducing LINQ. Agenda of LINQ Presentation We have features for every step of the way LINQ Fundamentals Anonymous Functions/Lambda.
XML files (with LINQ). Introduction to LINQ ( Language Integrated Query ) C#’s new LINQ capabilities allow you to write query expressions that retrieve.
LINQ Programming in C# LINQ CSE Prof. Roger Crawfis.
 Introduction  What is LINQ  Syntax  How to Query  Example Program.
Slides from Gang Luo, Xuting Zhao and Damien Guard
LINQ, An IntroLINQ, An Intro Florin−Tudor Cristea, Microsoft Student Partner.
Eric Vogel Software Developer A.J. Boggs & Company.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Based on material from Telerik Corporation.
Introduction to LINQ Lecture # 19 August Introduction How do you interrogate/manipulate data? What if you could do the work in a type-safe," string-free.
Ronnie Saurenmann Principal Architect Microsoft Switzerland.
LINQ and C# 3.0 Mads Torgersen Program Manager for the C# Language Microsoft Corporation.
LINQ: It’s Not Your Father’s Data Access Denny Boynton Anheuser-Busch Companies.
The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation.
© Satyam Computer Services Ltd POC Architecture 1 Confidential – Limited Circulation.
Neal Stublen How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,
11 Using ADO.NET II Textbook Chapter Getting Started Last class we started a simple example of using ADO.NET operations to access the Addresses.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
 Although VERY commonly used, arrays have limited capabilities  A List is similar to an array but provides additional functionality, such as dynamic.
Introduction to LINQ Chapter 11. Introduction Large amounts of data are often stored in a database—an organized collection of data. A database management.
1.NET Language Integrated Query Yishai Zaltzberg.
1 LinQ Introduction. Outline Goals of LinQ Anatomy of a LinQ query More expression examples LinQ to Objects LinQ to XML LinQ to SQL 2.
C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.
AUC Technologies LINQ (Language Integrated Query) LINQ Presented By : SHAIKH SHARYAR JAVED Software Engineer (Daedalus Software Inc.) Technology Teacher.
Language Integrated Query (LINQ). Data Access Programming Challenges Developers must learn data store-specific query syntax Multiple, disparate data stores.
C#: Future Directions in Language Innovation Anders Hejlsberg TLN307 Technical Fellow Microsoft Corporation.
Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
LINQ Language Integrated Query LINQ1. LINQ: Why and what? Problem Many data sources: Relational databases, XML, in-memory data structures, objects, etc.
Language Integrated Query Mike Taulty Developer & Platform Group Microsoft Ltd
LINQ and Lambda Expressions Telerik Software Academy LINQ Overview.
Functional Programming Data Aggregation and Nested Queries Ivan Yonkov Technical Trainer Software University
Building Web Applications with Microsoft ASP
Part 1: Overview of LINQ Intro to LINQ Presenter: PhuongNQK.
Introduction to LINQ and Generic Collections
Introduction to PL/SQL Programing
Intro to LINQ Part 2 – Concepts and LINQ to Objects
Language Integrated Query: (LINQ) An introduction
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
An Introduction to Entity Framework
Introduction to LINQ Chapter 11 10/28/2015 Lect 4 CT1411.
Language Integrated Query (LINQ)
Introduction to LINQ Chapter 11.
Language Integrated Query (LINQ)
Advances for Data in VS “Orcas”
Advances for Data in VS “Orcas”
ADO.NET Entity Framework
LINQ - 2 Ravi Kumar C++/C# Team.
Advanced .NET Programming I 6th Lecture
Chengyu Sun California State University, Los Angeles
CS4540 Special Topics in Web Development LINQ to Objects
Presentation transcript:

C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College

Agenda Introduction C# 3.0 New Features Introduction to LINQ LINQ to Objects LINQ to SQL LINQ to XML Summary 2©2008 Pavel Yosifovich

.NET Roadmap YearCLR.NETVisual StudioC# NET 3.0 ext ?

C# 3.0 Features Implicitly Typed Local Variables Automatic Properties Object and Collection Initializers Anonymous Types Extension Methods Lambda Expressions LINQ 4©2008 Pavel Yosifovich

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 5©2008 Pavel Yosifovich

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; } } 6©2008 Pavel Yosifovich

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 }; 7©2008 Pavel Yosifovich

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 } }; 8©2008 Pavel Yosifovich

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);// ??? 9©2008 Pavel Yosifovich

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)); 10©2008 Pavel Yosifovich

EXTENSION METHODS DEMO

Problem Strongly typed, Intellisense, Compilers, Debuggers (Imperative) Projection, Join, Grouping, Queries (Declarative) Data as Objects Objects as Data 12©2008 Pavel Yosifovich

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 13©2008 Pavel Yosifovich

LINQ enabled ADO.NET LINQ Architecture XML Objects Relational Data.NET Language-Integrated Query (LINQ) 14©2008 Pavel Yosifovich

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 15©2008 Pavel Yosifovich

LINQ To Objects Working with collections –Any one that implements IEnumerable using System.Linq System.Core.Dll assembly Deferred Execution

LINQ TO OBJECTS DEMO

Classic ADO.NET SqlConnection conn = new SqlConnection(“...“); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText SELECT * FROM Vehicles WHERE 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 “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

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

LINQ TO SQL DEMO

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 (…)

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 Yosifovich22

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

LINQ TO XML DEMO

©2008 Pavel Yosifovich25 Q & A ?

Summary LINQ Allows using data as objects and vice versa Same syntax across any provider C# Language support Use it today!