1. Trang chủ
  2. » Công Nghệ Thông Tin

a0017 whitepaper lin morebook vn 5914

7 3 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Language Integrated Query (LINQ) An Overview White Paper Sachin K Vishwakarma ( 247841 ) Confidentiality Statement © 2009 TATA CONSULTANCY SERVICES LIMITED All rights reserved This document contains confidential information of TATA CONSULTANCY SERVICES LIMITED, which is provided for the sole purpose of permitting the recipient to evaluate any proposal submitted herewith or separately In consideration of receipt of this document, the recipient agrees to maintain such information in confidence and to not reproduce or otherwise disclose this information to any person outside the group directly responsible for evaluation of its contents, except that there is no obligation to maintain the confidentiality of any information which was known to the recipient prior to receipt of such information from TATA CONSULTANCY SERVICES LIMITED, or becomes publicly known through no fault of recipient, or is received without obligation of confidentiality from a third party owing no obligation of confidentiality to TATA CONSULTANCY SERVICES LIMITED This document is the property of TATA CONSULTANCY SERVICES LIMITED and TATA CONSULTANCY SERVICES LIMITED may require the return of this document at any time at their sole discretion Unauthorized access or copying is prohibited with to the prior written permission of TATA CONSULTANCY SERVICES LIMITED All product and company names mentioned herein may be trademarks of their respective owners About the Author Having years experience in Developing application in net Contents INTRODUCTION Introduction to LINQ Queries The Data Source Query LINQ TO OBJECTS LINQ TO XML LINQ TO DATASET Distinct 10 Except 12 Intersect 12 Union 12 LINQ TO SQL 12 SUMMARY 14 Introduction As the Microsoft NET platform, and its supporting languages C# and VB, have matured, it has become apparent that one of the more troublesome areas still remaining for developers is that of accessing data from different data sources In particular, database access and XML manipulation is often cumbersome at best, and problematic in the worst cases The database problems are numerous First, there is the issue that we cannot programmatically interact with a database at the native language level This means syntax errors often go undetected until runtime Incorrectly referenced database fields are not detected either This can be disastrous, especially if this occurs during the execution of error-handling code Nothing is more frustrating than having an entire error-handling mechanism fail because of syntactically invalid code that has never been tested Sometimes this is unavoidable due to unanticipated error behavior Having database code that is not validated at compile time can certainly lead to this problem A second problem is the nuisance caused by the differing data types utilized by a particular data domain, such as database or XML data types versus the native language in which the program is written In particular, dates and times can be quite a hassle XML parsing, iterating, and manipulation can be quite tedious Often an XML fragment is all that is desired, but due to the W3C DOM XML API, an XmlDocument must be created just to perform various operations on the XML fragment Rather than just add more classes and methods to address these deficiencies in a piecemeal fashion, the development team at Microsoft decided to go one step further by abstracting the fundamentals of data query from these particular data domains The result was LINQ LINQ is Microsoft's technology to provide a language-level support mechanism for querying data of all types These types include in-memory arrays and collections, databases, XML documents, and more Language-Integrated Query (LINQ) is a technology that bridges the gap between front end and back end LINQ can be used with any data source Developers can express efficient query behavior in their programming language of choice, optionally transform/shape data query results into whatever format they want, and then easily manipulate the results LINQenabled languages such as c# and vb can provide compile-time checking, thus full type safety of query expressions and development tools can provide debugging, intellisense and rich refactoring Introduction to LINQ Queries A query is an expression that retrieves data from a data source Queries are usually expressed in a specialized query language Different languages have been developed over time for the various types of data sources, for example SQL for relational databases and XQuery for XML Therefore, developers have had to learn a new query language for each type of data source or data format that they must support LINQ simplifies this situation by offering a consistent model for working with data across various kinds of data sources and formats In a LINQ query, you are always working with objects You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, NET collections, and any other format for which a LINQ provider is available // Create the DataContext Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind"); // Instantiate an entity object Customer cust = new Customer { CustomerID = "LAWN", CompanyName = "Lawn Wranglers", ContactName = "Mr Abe Henry", ContactTitle = "Owner", Address = "1017 Maple Leaf Way", City = "Ft Worth", Region = "TX", PostalCode = "76104", Country = "USA", Phone = "(800) MOW-LAWN", Fax = "(800) MOW-LAWO" }; // Add the entity object to the Customers table db.Customers.InsertOnSubmit(cust); // Call the SubmitChanges method db.SubmitChanges(); Select Query : Performing a Single LINQ to SQL Query : - Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind"); Customer cust = (from c in db.Customers where c.CustomerID == "LONEP" select c).Single(); INQ to SQL Query Returning an IQueryable Sequence : Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind"); IQueryable custs = from c in db.Customers where c.City == "London" select c; foreach(Customer cust in custs) { Console.WriteLine("Customer: {0}", cust.CompanyName); } Delete :To delete a record from a database using LINQ to SQL, you must delete the entity object from the Table of which it is a member with the Table object's DeleteOnSubmitmethod Then, of course, you must call the SubmitChanges method Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind"); // Retrieve a customer to delete Customer customer = (from c in db.Customers where c.CompanyName == "Alfreds Futterkiste" select c).Single(); db.OrderDetails.DeleteAllOnSubmit( customer.Orders.SelectMany(o => o.OrderDetails)); db.Orders.DeleteAllOnSubmit(customer.Orders); db.Customers.DeleteOnSubmit(customer); db.SubmitChanges(); Summary LINQ raises the impression (further development assumed) to solve a big and wide spread problem: the paradigm change between relational and object model Now that object oriented programming has already reached a stable point it is only a question of time when the according data access methods would get thought over The more followers this new technology will get, the more time and cost will be invested by the programming industry and the usability as well as the performance of emerging competitor products will arise Even now there exist object to relational mappers of other originators The deficit of these is simply that they are not embedded in any programming language ... Introduction to LINQ Queries The Data Source Query LINQ TO OBJECTS LINQ TO XML LINQ TO DATASET ... especially if this occurs during the execution of error-handling code Nothing is more frustrating than having an entire error-handling mechanism fail because of syntactically invalid code that... abstracting the fundamentals of data query from these particular data domains The result was LINQ LINQ is Microsoft''s technology to provide a language-level support mechanism for querying data

Ngày đăng: 04/12/2022, 09:46

Xem thêm:

w