C# LINQ

4,094 views

Published on

Published in: Education, Technology
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,094
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
0
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

C# LINQ

  1. 1. LINQWhat is LINQ?Introduction to LINQLINQ Example
  2. 2. Language Integrated Query Language-Integrated Query (LINQ) is an innovation introduced in Visual Studio 2008 and .NET Framework version 3.5 that bridges the gap between the world of objects and the world of data. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support. Furthermore, you have to learn a different query language for each type of data source: SQL databases, XML documents, various Web services, and so on. LINQ makes a query a first-class language construct in C# and Visual Basic. You write queries against strongly typed collections of objects by using language keywords and familiar operators.
  3. 3. Language Integrated Query
  4. 4. 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. 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
  5. 5. Example LINQ to Collections class IntroToLINQ { static void Main() { int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 }; // 2. Query creation. var numQuery = from num in numbers where (num % 2) == 0 orderby num descending select num; // 3. Query execution. foreach (int num in numQuery) { Console.Write("{0,1} ", num); } }}
  6. 6. Advantages of LINQ  Unified data access Single syntax to learn and remember  Strongly typed Catch errors during compilation  IntelliSense Prompt for syntax and attributes  Bindable result sets
  7. 7. LINQ ArchitectureC# VB.NET Others.NET Language Integrated Query (LINQ) LINQ data source providers ADO.NET support for LINQLINQto LINQ LINQ LINQ LINQObjects to Datasets to SQL to Entities to XML
  8. 8. LINQ to SQL  Object-relational mapping Records become strongly-typed objects  Data context is the controller mechanism  Facilitates update, delete & insert  Translates LINQ queries behind the scenes  Type, parameter and injection safe
  9. 9. Database mapping  VS 2008 designer or SQLMetal command  Map tables & fields to classes & properties  Generates partial classes with attributes  Each record becomes an object  Data context represents the database  Utilize tables, views or stored procedures
  10. 10. Modifying objects  Update Set object properties  Delete context.Table.DeleteOnSubmit(object)  Insert context.Table.InsertOnSubmit(object)  Commit changes back context.SubmitChanges() Transactional - all or nothing

×