LINQ Programming in C# LINQ CSE 459.24 Prof. Roger Crawfis.

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
Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training
LINQ and Collections An introduction to LINQ and Collections.
Michael Pizzo Software Architect Data Programmability Microsoft Corporation.
Visual Studio 2008 and the.NET Framework v3.5 Gill Cleeren Microsoft Regional Director.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
C# and LINQ Yuan Yu Microsoft Research Silicon Valley.
1 Visual Studio 2008 Fons Sonnemans (Trainer) Reflection IT
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.
C# 3.0 & LINQ Raimond Brookman – IT Architect
Damien Guard (BSc, MBCS) Guernsey Software Developer Forum Language Integrated Query:
LINQ: Language-Integrated Queries (To be included in C # 3.0) Technology developed by Anders Hejlsberg & friends at Microsoft (2005) Presented by Tal Cohen.
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.
The Microsoft Technical Roadshow 2007 Advances for Data in VS “Orcas” Mike Taulty Developer & Platform Group Microsoft Ltd
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.
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.
 Introduction  What is LINQ  Syntax  How to Query  Example Program.
Slides from Gang Luo, Xuting Zhao and Damien Guard
Reflection IT LINQ & Entity Framework Fons Sonnemans (Trainer)
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
LINQ, An IntroLINQ, An Intro Florin−Tudor Cristea, Microsoft Student Partner.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 11 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# 3.0 and.NET 3.5: A Brief Overview Pavel Ježek.
Getting familiar with LINQ to Objects Florin−Tudor Cristea, Microsoft Student Partner.
Putting it all together: LINQ as an Example. The Problem: SQL in Code Programs often connect to database servers. Database servers only “speak” SQL. Programs.
LINQ TO XML Mike Taulty Developer & Platform Group Microsoft UK
Advanced C#, part IV Niels Hallenberg IT University of Copenhagen (With thanks to Peter Sestoft and Kasper Østerbye) BAAAP – Spring 2009.
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.
Neal Stublen How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,
 Language Integrated Query  Make query a part of the language  Component of.NET Framework 3.5  Shipped with Visual Studio 2008.
LINQ Providers Or why.NET rules, and Java doesn’t Branimir Giurov SofiaDev.org UG Lead, C# MVP Freelance Software Developer
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.
Language Integrated Query (LINQ). Data Access Programming Challenges Developers must learn data store-specific query syntax Multiple, disparate data stores.
By: Luis Carranco CIS764 - Fall  What is LINQ  Architecture  How does it work?  Samples/Demo  Why to use LINQ? 2.
C#: Future Directions in Language Innovation Anders Hejlsberg TLN307 Technical Fellow Microsoft Corporation.
Applied Linq Putting Linq to work Introducing… Class-A Kennisprovider Microsoft development Training Coaching Alex Thissen Trainer/coach.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
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
Linq Overview Vincent GERMAIN. Evolution - Rappel Langage  C# 2.0  C# 3.0 (Local type inference, Lambda expression, Method extension,Anonymous type)
Damien Guard (BSc, MBCS) Guernsey Software Developer Forum Language Integrated Query:
Joel Pobar Language Geek Microsoft DEV320 Improve on C# % Backwards Compatible Language Integrated Query (LINQ)
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.
Ken Casada Developer Evangelist Microsoft Switzerland
Language Integrated Query Mike Taulty Developer & Platform Group Microsoft Ltd
LINQ and Lambda Expressions Telerik Software Academy LINQ Overview.
Sergiy Baydachnyy Developer Evangelist Microsoft Corporation.
Chapter 11.  Large amounts of data are often stored in a database—an organized collection of data.  A database management system (DBMS) provides mechanisms.
Part 1: Overview of LINQ Intro to LINQ Presenter: PhuongNQK.
Intro to LINQ Part 2 – Concepts and LINQ to Objects
Language Integrated Query: (LINQ) An introduction
LINQ  LINQ – Language INtegrated Query  ( заявки вградени в езика )
Advanced .NET Programming I 6th Lecture
Visual Studio “Orcas” & .NET Framework v3.5
Language Integrated Query (LINQ)
Advances for Data in VS “Orcas”
Advances for Data in VS “Orcas”
LINQ - 2 Ravi Kumar C++/C# Team.
Entity Framework & LINQ (Language Integrated Query)
Advanced .NET Programming I 6th Lecture
CS4540 Special Topics in Web Development LINQ to Objects
Presentation transcript:

LINQ Programming in C# LINQ CSE Prof. Roger Crawfis

First, A Taste of LINQ using System; using System.Query; using System.Collections.Generic; class app { static void Main() { string[] names = { "Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David" }; var expr = from s in names where s.Length == 5 orderby s select s.ToUpper(); foreach (string item in expr) Console.WriteLine(item); } BURKE DAVID FRANK

Queries without LINQ foreach(Customer c in customers) if (c.Region == "USA")... Objects using loops and conditions XML using XPath/XQuery SELECT * FROM Customers WHERE Region='USA' SELECT from database tables

Key Features of LINQ Delayed Execution LINQ queries don't execute until they must Retrieve specific values Iterate through the collection Perform an operation Write Data Access Code directly Compile time syntax and schema checking (intelli-sense too) No need for inline sql and to wait until runtime to see if it is ok LINQ data access code abstracted from underlying data Consistent syntax across various data sources Can join information from different sources.

Language INtegrated Query LINQ enabled data sources LINQ To Objects Objects LINQ To XML XML LINQ enabled ADO.NET LINQ To Datasets LINQ To SQL LINQ To Entities Relational Others… VB C#.NET Language-Integrated Query LINQ provides one programming model for all types of data (objects, SQL, XML, DataSets)

LINQ Enabled Data Sources LINQ to Objects LINQ to SQL (formerly known as DLINQ) LINQ to XML (formerly known as XLINQ) LINQ to Entities (ADO.NET Entities)

LINQ to Objects Query any IEnumerable source Includes arrays, List, Dictionary... Many useful operators available Sum, Max, Min, Distinct, Intersect, Union Expose your own data with IEnumerable or IQueryable Create operators using extension methods

Lambda Expressions Common LINQ Uses Predicate (p) => p.Gender == “F” “All persons, p, such that person’s Gender is F” Projection (p) => p.Gender == “F” ? “Female” : “Male” Each person p becomes string “Female” or “Male” Technically wrong: The predicate will return true or false.

Query Expressions Introduce SQL-Like Syntax to Language Compiled to Traditional C# (via Extension Methods) from itemName in srcExpr join itemName in srcExpr on keyExpr equals keyExpr (into itemName)? let itemName = selExpr where predExpr orderby (keyExpr (ascending | descending)?)* select selExpr group selExpr by keyExpr into itemName query-body

using System; using System.Query; using System.Collections.Generic; class app { static void Main() { string[] names = { "Allen", "Arthur", "Bennett" }; IEnumerable ayes = names.Where(s => s[0] == 'A'); foreach (string item in ayes) Console.WriteLine(item); names[0] = "Bob"; foreach (string item in ayes) Console.WriteLine(item); } Arthur LINQ to Objects Query Operators can be used against any.NET collection (IEnumerable ) Select, Where, GroupBy, Join, etc. Deferred Query Evaluation Lambda Expressions using System; using System.Query; using System.Collections.Generic; class app { static void Main() { string[] names = { "Allen", "Arthur", "Bennett" }; IEnumerable ayes = names.Where(s => s[0] == 'A'); foreach (string item in ayes) Console.WriteLine(item); names[0] = "Bob"; foreach (string item in ayes) Console.WriteLine(item); } Allen Arthur using System; using System.Query; using System.Collections.Generic; class app { static void Main() { string[] names = { "Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David" }; IEnumerable expr = from s in names where s.Length == 5 orderby s select s.ToUpper(); foreach (string item in expr) Console.WriteLine(item); } BURKE DAVID FRANK using System; using System.Query; using System.Collections.Generic; class app { static void Main() { string[] names = { "Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David" }; Func filter = s => s.Length == 5; Func extract = s => s; Func project = s = s.ToUpper(); IEnumerable expr = names.Where(filter).OrderBy(extract).Select(project); foreach (string item in expr) Console.WriteLine(item); } BURKE DAVID FRANK

LINQ Uses Extension Methods

LINQ to SQL Overview ORM Designer Maps Relational Structures to Classes Delay Load for Expensive Values Uses the IQuerably interface rather than the IEnumerable.

Architecture Compilers (language-level support) Standard Query Operators Expression Trees Linq-enabled data sources Linq to objects Linq-enabled ADO.NET Linq to SQL (DLinq) Linq to datasets Linq to entities Linq to XML (XLinq)

LINQ to SQL Architecture ApplicationApplication LINQ to SQL SQL Server from c in db.Customers where c.City == "London" select c.CompanyName from c in db.Customers where c.City == "London" select c.CompanyName EnumerateEnumerate SELECT CompanyName FROM Customer WHERE City = 'London' SELECT CompanyName FROM Customer WHERE City = 'London' SQL Query or SProc RowsRows ObjectsObjects db.Customers.Add(c1); c2.City = “Seattle"; db.Customers.Remove(c3); db.Customers.Add(c1); c2.City = “Seattle"; db.Customers.Remove(c3); SubmitChanges()SubmitChanges() INSERT INTO Customer … UPDATE Customer … DELETE FROM Customer … INSERT INTO Customer … UPDATE Customer … DELETE FROM Customer … DML or SProcs

Expression Trees Expressions as objects LambdaExpression BinaryExpression (Add) BinaryExpression (Multiply) UnaryExpression (Negate)ParameterExpression (b) Body Right Left ParameterExpression (a) Operand ParameterExpression (c) -a * b + c LeftRight

Expression Trees In-memory tree representation of LINQ Query Interpreted to specific data source by specific IQueryProvider like SQLQueryProvider

IQueryable and IQueryProvider Queryable C# 3.0 Query IQueryProvider T-SQL Statements C# 3.0 Compiler

Enumerable vs. Queryable Both are static classes Both contain extension methods Enumerable extends IEnumerable Queryable extends IQueryable

Enumerable vs. Queryable Enumerable Func<> delegate as method parameter Intended for in-memory sequence iteration Invokes the delegate as-is public static IEnumerable Select ( this IEnumerable source, Func selector ); public static IEnumerable Select ( this IEnumerable source, Func selector );

Enumerable vs. Queryable Queryable Expression tree as method parameter Expression tree interpreted to specific data source by IQueryProvider There is no “real” delegate public static IQueryable Select ( this IQueryable source, Expression > selector ); public static IQueryable Select ( this IQueryable source, Expression > selector );

LINQ Examples More of a Database course topic. See LINQ 101 examples. Aggregate Operators Projection Operators

LINQ Examples LINQ to XML: See brooks/LINQ/readingXML.htm brooks/LINQ/readingXML.htm