LINQ Language Integrated Query LINQ1. LINQ: Why and what? Problem Many data sources: Relational databases, XML, in-memory data structures, objects, etc.

Slides:



Advertisements
Similar presentations
The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd
Advertisements

XML: Extensible Markup Language
LINQ and Collections An introduction to LINQ and Collections.
Michael Pizzo Software Architect Data Programmability Microsoft Corporation.
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#
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
The XML data type in Microsoft SQL Server. The XML Type Microsoft SQL Server offers a special data type – XML – Used in tables, etc. – Basically a (long)
Summary. Chapter 9 – Triggers Integrity constraints Enforcing IC with different techniques –Keys –Foreign keys –Attribute-based constraints –Schema-based.
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
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
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.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Entity Framework MIS 324 MIS 324 Professor Sandvig Professor Sandvig.
Chapter 15: Using LINQ to Access Data in C# Programs.
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.
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.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Neal Stublen How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,
Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.
Concepts of Database Management Seventh Edition
 Language Integrated Query  Make query a part of the language  Component of.NET Framework 3.5  Shipped with Visual Studio 2008.
Optimization in XSLT and XQuery Michael Kay. 2 Challenges XSLT/XQuery are high-level declarative languages: performance depends on good optimization Performance.
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.
[ Part III of The XML seminar ] Presenter: Xiaogeng Zhao A Introduction of XQL.
JSTL The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications.
C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
By: Luis Carranco CIS764 - Fall  What is LINQ  Architecture  How does it work?  Samples/Demo  Why to use LINQ? 2.
Applied Linq Putting Linq to work Introducing… Class-A Kennisprovider Microsoft development Training Coaching Alex Thissen Trainer/coach.
Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann
XML Query: xQuery Reference: Xquery By Priscilla Walmsley, Published by O’Reilly.
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.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
SQL Server 2005 XML Datatype David Wilson Ohio North SQL Server Special Interest Group July 12, 2007.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
CIS 200 Test 01 Review. Built-In Types Properties  Exposed “Variables” or accessible values of an object  Can have access controlled via scope modifiers.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 11.  Large amounts of data are often stored in a database—an organized collection of data.  A database management system (DBMS) provides mechanisms.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Part 1: Overview of LINQ Intro to LINQ Presenter: PhuongNQK.
Introduction to LINQ and Generic Collections
CIS 200 Test 01 Review.
Visual Basic 2010 How to Program
The University of Texas – Pan American
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Introduction to LINQ Chapter 11 10/28/2015 Lect 4 CT1411.
Language Integrated Query (LINQ)
CIS16 Application Programming with Visual Basic
Introduction to LINQ Chapter 11.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections UTPA – Fall 2012 This set of slides is revised from lecture slides.
Advances for Data in VS “Orcas”
Generics in C# / Anders Børjesson
Advances for Data in VS “Orcas”
LINQ - 2 Ravi Kumar C++/C# Team.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
CS4540 Special Topics in Web Development LINQ to Objects
Presentation transcript:

LINQ Language Integrated Query LINQ1

LINQ: Why and what? Problem Many data sources: Relational databases, XML, in-memory data structures, objects, etc. Each data source has an access language: SQL, DOM, etc. Solution LINQ One unified access language working on all kinds of data sources The name Language Integrated Some new keywords was introduced to the C# programming language The C# compiler checks the syntax Unlike SQL, which is just a test string to the C# compiler Query It’s all about querying data source Getting data out of the data source LINQ2

Data sources In-memory data sources Arrays, int[] data = new int[] {3,4,5,6}; List ArrayList Special “version”: LINQ to Objects Relational databases Special “version”: LINQ to SQL XML documents Special “version”: LINQ to XML LINQ3

LINQ basic syntax: Comparable to SQL LINQ From product in products Where condition Select new { product.Name, product.Price } Starting with the from clause makes is easier to use IntelliSense help SQL Select Name, Price From products Where condition LINQ4

Filtering From … where condition … select … The where clause is optional If it is missing you select every element from the data source The condition must evaluate to either true or false Example: linqFirstTry -> CheapProducts LINQ5

Projection using the select clause Decide which properties should be returned Syntax From … where condition … select … Select the whole object Select a single property Select a number of properties Use Anonymous types From p in P Select new {p.prop1, p.prop2} Example: linqFirstTry -> CheapProducts + SelectExample LINQ6

Ordering Select … where … orderby … direction Direction may be either ascending or descending Default is ascending Sorting by more than one attribute If is possible to sort by more than one attribute Orderby primary, secondary, etc. Example linqFirstTry -> Ordering + JoiningMemoryUsers LINQ7

Non-generic data source Selecting from a non-generic data source, like an ArrayList, requires a little extra ArrayList arrayList = new ArrayList(); arrayList.Add("Anders"); arrayList.Add("John"); arrayList.Add("Peter"); var longNames = from String text in arrayList where (text.Length >= 5) select text; If the data source happens to have members other than the specified type, the program compiles but you get an InvalidCastException at runtime arrayList.add(47) → InvalidCastException Conclusion: Try to avoid non-generic data sources! LINQ8

LINQ with expression syntax Ordinary LINQ Special keywords From … where … select LINQ with expression syntax Calling methods on IEnumerable Example: linqFirstTry -> main -> QuerySyntaxExamples LINQ9

Type relationships LINQ10 Source:

Join Database Join = select from more tables LINQ Select from more data sources Example LinqFirstTry -> Joining LinqFirstTry -> JoiningMemoryUsers LINQ11

LINQ on Databases LINQ to SQL LINQ is converted to SQL, which is sent to the database management system Example LinqToSqlNorthwind LINQ12

LINQ on XML documents LINQ to XML XML is untyped (if you are not using an XML schema) It is basically text strings Strings must be converted to numbers, etc. Example: LinqToXmlTrying LINQ13

References and further readings MSDN LINQ (Language-Integrated Query) John Sharp Visual C# 2012, Microsoft Press 2012 Chapter 21 Querying In-Memory Data by Using Query Expressions, page Chapter 24 Improving Response Time by Performing Asynchronous Operations Using PLINQ to Parallelize Declarative Data Access, page Bart De Smet: C# 5.0 Unleashed, Sams 2013 Chapter 19 Language Integrated Query Essentials, page LINQ14