
- •Contents
- •Lab 1: linq Project: Unified Language Features for Object and Relational Queries
- •Lab Objective
- •Exercise 1 –linq for In-Memory Collections
- •Task 1 – Creating the “linq Overview” Solution
- •Task 2 – Querying a Generic List of Integers
- •Task 3 – Querying Structured Types
- •Exercise 2 – linq to xml: linq for xml documents
- •Task 1 – Adding linq to xml Support
- •Task 2 – Querying by Reading in an xml File
- •Task 3 – Querying an xml File
- •Task 4 – Transforming xml Output
- •Exercise 3 – linq to DataSet: linq for DataSet Objects
- •Task 1 – Adding DataSet Support
- •Task 2 – Creating a DataSet – Add Designer File
- •Task 3 – Creating a DataSet – Using the Designer
- •Task 4 – Querying a DataSet
- •Exercise 4 – linq to sql: linq for Connected Databases
- •Task 1 – Adding linq to sql Support
- •Task 2 – Creating Object Mapping – Creating an Object and Providing Attributes
- •Task 3 – Creating Object Mapping – Using the Designer – Add Designer File
- •Task 4 – Creating Object Mapping – Using the Designer – Create the Object View
- •Task 5 – Querying using Expressions
- •Task 6 – Modifying Database Data
- •Task 7 – Calling Stored Procedures
- •Task 8 – Expanding Query Expressions
- •Exercise 5 – Understanding the Standard Query Operators [Optional]
- •Task 1 – Querying using the Standard Query Operators
- •Task 2 – Working with the Select Operator
- •Task 3 – Working with the Where Operator
- •Task 4 – Working with the Count Operator
- •Task 5 – Working with the Min, Max, Sum, and Average Operators
- •Task 6 – Working with the All and Any operators
- •Task 7 – Working with the ToArray and ToList Operators
- •Lab Summary
L
INQ:
Language Integrated Query
Hands-on Lab
November 2007
For the latest information, please see www.microsoft.com/vstudio
Information in this document is subject to change without notice. The example companies, organizations, products, people, and events depicted herein are fictitious. No association with any real company, organization, product, person or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarked, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.
© 2007 Microsoft Corporation. All rights reserved.
Microsoft, MS-DOS, MS, Windows, Windows NT, MSDN, Active Directory, BizTalk, SQL Server, SharePoint, Outlook, PowerPoint, FrontPage, Visual Basic, Visual C++, Visual J++, Visual InterDev, Visual SourceSafe, Visual C#, Visual J#, and Visual Studio are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.
Other product and company names herein may be the trademarks of their respective owners.
Contents
Lab 1: LINQ Project: Unified Language Features for Object and Relational Queries 1
Lab Objective 1
Exercise 1 –LINQ for In-Memory Collections 2
Task 1 – Creating the “LINQ Overview” Solution 2
Task 2 – Querying a Generic List of Integers 2
Task 3 – Querying Structured Types 4
Exercise 2 – LINQ to XML: LINQ for XML documents 6
Task 1 – Adding LINQ to XML Support 6
Task 2 – Querying by Reading in an XML File 6
Task 3 – Querying an XML File 7
Task 4 – Transforming XML Output 8
Exercise 3 – LINQ to DataSet: LINQ for DataSet Objects 9
Task 1 – Adding DataSet Support 9
Task 2 – Creating a DataSet – Add Designer File 10
Task 3 – Creating a DataSet – Using the Designer 10
Task 4 – Querying a DataSet 10
Exercise 4 – LINQ to SQL: LINQ for Connected Databases 11
Task 1 – Adding LINQ to SQL Support 12
Task 2 – Creating Object Mapping – Creating an Object and Providing Attributes 12
Task 3 – Creating Object Mapping – Using the Designer – Add Designer File 13
Task 4 – Creating Object Mapping – Using the Designer – Create the Object View 13
Task 5 – Querying using Expressions 14
Task 6 – Modifying Database Data 15
Task 7 – Calling Stored Procedures 17
Task 8 – Expanding Query Expressions 17
Exercise 5 – Understanding the Standard Query Operators [Optional] 19
Task 1 – Querying using the Standard Query Operators 19
Task 2 – Working with the Select Operator 20
Task 3 – Working with the Where Operator 22
Task 4 – Working with the Count Operator 23
Task 5 – Working with the Min, Max, Sum, and Average Operators 24
Task 6 – Working with the All and Any operators 25
Task 7 – Working with the ToArray and ToList Operators 26
Lab Summary 28
Lab 1: linq Project: Unified Language Features for Object and Relational Queries
This lab provides an introduction to the LINQ Project. The language integrated query framework for .NET (“LINQ”) is a set of language extensions to C# and Visual Basic and a unified programming model that extends the .NET Framework to offer integrated querying for objects, databases, and XML.
In this lab, you will see how LINQ features can be used against in-memory collections, XML documents, and connected databases. The lab ends with an optional exercise that looks at the various standard query operators available for data manipulation and extraction.
This lab makes extensive use of new language features provided in C# 3.0. When first introduced they are pointed out; however for a more in depth understanding of how these features work, refer to the C# 3.0 Language Specification Hands On Lab.
Lab Objective
Estimated time to complete this lab: 60 minutes
The objective of this lab is to provide a clear understanding of the LINQ project. You will see how data manipulation can occur on objects in memory, XML files, datasets, and relational databases. The new LINQ APIs benefit from IntelliSense™ and full compile-time checking without resorting to string-based queries. This lab touches on basic LINQ technologies, along with database-specific LINQ to SQL, XML-specific LINQ to XML, and dataset-specific LINQ to DataSets. A brief look at query operators is also included.
This lab uses the Northwind database and consists of the following exercises:
LINQ to Objects: LINQ for In-Memory Collections LINQ to XML: LINQ for XML Documents LINQ to DataSets: LINQ for Dataset Objects LINQ to SQL: LINQ for Connected DatabasesUnderstanding the Standard Query Operators [Optional] |