Published on

An introduction on LINQ

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Th
  • Language Integrated Query ( LINQ , pronounced "link") is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages using a syntax reminiscent of SQL. Many of the concepts that LINQ has introduced were originally tested in Microsoft's Cω research project. LINQ was released as a part of .NET Framework 3.5 on November 19, 2007. LINQ is available for any .NET languages. Through visual Studio you can find support for C#, VB.NET. LINQ is a uniform programming model for any kind of data it introduces queries as first class concept in any .NET languages.
  • You can write LINQ queries against various enumerable data sources (that is, a data source that implements the IEnumerable interface). Examples are in-memory data structures (Collections, Lists, etc.,), XML documents, SQL databases, and  DataSet objects. LINQ to SQ L: Allows to querying a relational structure by converting LINQ query into a native SQL Query. LINQ to Dataset : Interacts with data already in ADO.NET DataSet structures. LINQ to Entity : To query a logical model that usually has a higher-level representation of data information compared to direct access to relational data sources. When LINQ query operates on data that is on relational database, the LINQ query generates an equivalent SQL statements.
  • The above query can be applied to SQL Database , DataSet, or an array of objects in memory or many other kinds of data. Here Customer could be a collection of objects. The SQL like syntax in LINQ called as query expressions . The reason to start with a from clause instead of a select statement, as in SQL syntax, is related to the need to provide Microsoft IntelliSense capabilities within the remaining part of the query, which makes writing conditions, selections, and any other LINQ query clauses easier.
  • At the core, LINQ works over anything that implements IEnumerable . Natively includes a number of lists that are part of the .NET Framework as well as lists you create. It also works with lists of elements and attributes present in XML documents as well as fields and tables present in DataSets. By extending the base IEnumerable with a new interface, IQueryable , we can add a layer which retains the query structure and has the ability to translate it into a language more native to the underlying data source (like TSQL for SQL Server). Out of the box, the .NET Framework ships with two such providers: LINQ to SQL and LINQ to Entities.
  • The where query operator defines the restriction to filter the collection. The Orderxx operators are used for sorting. ThenBy and ThenByDescending operators are used for a secondary sort if the first sort gives similar result.
  • The where query operator defines the restriction to filter the collection. The Orderxx operators are used for sorting. ThereBy and ThereByDescending operators are used for a secondary sort if the first sort gives similar result.
  • Add some more stuff on Extending LINQ (CVM)
  • Linq

    1. 1. Language Integrated Query Ch. Vishwa Mohan Freelance Software Consultant & Corporate Trainer
    2. 2. Table of Contents <ul><li>Introducing LINQ </li></ul><ul><li>New Language Enhancements in C# 3.0 </li></ul><ul><li>Language Fundamentals </li></ul><ul><li>Advanced Query Operators in LINQ </li></ul><ul><li>LINQ to ADO.NET </li></ul><ul><li>LINQ to XML </li></ul><ul><li>The Entity Framework </li></ul>
    3. 3. Introducing LINQ <ul><li>What is LINQ ? </li></ul><ul><ul><li>Stands for Language INtegrated Query . </li></ul></ul><ul><ul><li>LINQ makes it possible to access different data source with the same syntax. </li></ul></ul><ul><ul><li>It is a part of Microsoft .NET Framework 3.5 component. </li></ul></ul><ul><ul><li>It adds native data querying capabilities to .NET language using a syntax reminiscent of SQL. </li></ul></ul><ul><ul><li>Allows developers to query data structures using SQL-like syntax from within their applications code. </li></ul></ul><ul><ul><li>LINQ is another tool for embedding SQL queries into code. </li></ul></ul><ul><ul><li>The key feature of LINQ is its integration with widely used programming language, mad possible by the usage of syntax common to all kind of content. </li></ul></ul><ul><li>LINQ provides consistent model for working with data across various kinds of data sources and formats. </li></ul><ul><ul><li>In a LINQ query, you are always working with objects. </li></ul></ul><ul><ul><li>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. </li></ul></ul>
    4. 4. LINQ Basics <ul><li>LINQ is a unified programming model for any kind of data. It can allow the developers to query and manage sequence of items ( Eg: In memory collections, tables in database, XML Nodes , and so on ). </li></ul><ul><ul><li>LINQ enable you to query and manipulate data with a consistent model that is independent from data source (May be SQL Server, XML file, Arrays or Lists, Text file, e-mail message, SOAP message, etc., ). </li></ul></ul><ul><ul><li>SQL Like syntax used in LINQ is called “ query expressions ”. </li></ul></ul><ul><li>LINQ queries are expressed in the programming language itself, and not as string literals embedded in the application code. So it offers several advantages: </li></ul><ul><ul><li>Eliminates the need of using separate language. </li></ul></ul><ul><ul><li>With Visual Studio 2008 IDE you will also benefit compile time checking, static typing and Intellisense support. </li></ul></ul><ul><li>LINQ to ADO.NET will implements the functionality needs to manipulate the relational data. It has the following modules: </li></ul><ul><ul><li>LINQ to SQL, LINQ to DataSet and LINQ to Entity </li></ul></ul>
    5. 5. LINQ Basics
    6. 6. LINQ Basics <ul><li>The LINQ family of technologies provides a consistent query experience for </li></ul><ul><ul><li>objects ( LINQ ), </li></ul></ul><ul><ul><li>relational databases ( LINQ to SQL ) </li></ul></ul><ul><ul><li>XML ( LINQ to XML ). </li></ul></ul>
    7. 7. Introducing LINQ <ul><li>All LINQ query operations consists of three distinct operations: </li></ul><ul><ul><li>Obtain the data source : </li></ul></ul><ul><ul><li>int[] mynumbers = new int[] { 49, 28, 20, 15, 25, 23, 24, 10, 7, 34 }; </li></ul></ul><ul><ul><ul><li>A LINQ data source is any object that supports the generic IEnumerable<T> interface, or an interface that inherits from it. </li></ul></ul></ul><ul><ul><ul><li>Types such as ArrayList that support the non-generic IEnumerable interface can also be used as a LINQ data source. ( But there is small change in query ) </li></ul></ul></ul><ul><ul><ul><li>var oddNumbers = from int num in mynumbers ..,//specify num type as int. </li></ul></ul></ul><ul><ul><li>Create the query : </li></ul></ul><ul><ul><li>var oddNumbers = from num in mynumbers </li></ul></ul><ul><ul><li>where (num % 2) != 0 </li></ul></ul><ul><ul><li>select num; </li></ul></ul><ul><ul><ul><li>The from clause specifies the data source, the where clause applies the filter, and the select clause specifies the type of the returned elements. </li></ul></ul></ul><ul><ul><li>// oddNumbers is an IEnumerable<int> . So you can use foreach on this. </li></ul></ul><ul><ul><li>Execute the query : </li></ul></ul><ul><ul><li>foreach (int num in oddNumbers) {Console.Write(&quot;{0,1} &quot;, num); } </li></ul></ul>
    8. 8. Introducing LINQ
    9. 9. LINQ Introduction <ul><li>LINQ query variables are typed as IEnumerable<T> or a derived type such as IQueryable<T> . Let us see the below query </li></ul><ul><li>var customerQuery = from cust in customers </li></ul><ul><li>where cust.City == &quot;London&quot; </li></ul><ul><li>select cust; </li></ul><ul><ul><li>Here var is a type of IEnumerable<Customer> . It means that when the query is executed it will produce sequence of zero or more Customer objects. </li></ul></ul><ul><li>Types that support IEnumerable<T> or a derived interface such as IQueryable<T> are called queryable types . You can create LINQ expression on these types. </li></ul><ul><ul><li>The types that implements IEnumerable ( Non generic version ) interface also be used as a LINQ data source. (Eg: ArrayList ). </li></ul></ul><ul><ul><li>A queryable type requires no modification or special treatment to serve as a LINQ data source. </li></ul></ul><ul><ul><li>If the source data is not already in memory as a queryable type, the LINQ provider must represent it as such. </li></ul></ul><ul><ul><li>Eg: LINQ to XML loads an XML document into a queryable  XElement  type:  </li></ul></ul>
    10. 10. LINQ – Language INtegrated Query
    11. 11. LINQ Syntax var customerQuery = from cust in customers where cust.City == &quot;London&quot; select cust;
    12. 12. LINQ Operators Classification
    13. 13. Standard Query Operators in LINQ <ul><li>LINQ query defines a declarative syntax for the most common operators. They are categorized as follow: </li></ul><ul><ul><li>Restriction Operators: where </li></ul></ul><ul><ul><ul><li>The where operator defines the restriction to filter the collection. </li></ul></ul></ul><ul><ul><li>Project Operators: select, selectMany </li></ul></ul><ul><ul><ul><li>These select and selectMany operators are defines a projection to select values of the result based on a selector function. </li></ul></ul></ul><ul><ul><li>Ordering Operators: OrderBy, OrderByDescending, ThenBy, ThenByDescending, Reverse. </li></ul></ul><ul><ul><ul><li>The OrderBy used to ascending sorting and OrderByDescending used for descending sorting. </li></ul></ul></ul><ul><ul><ul><li>The ThenBy and TheyByDescending operators are used for a secondary sort if the first sort gives a similar result. </li></ul></ul></ul><ul><ul><ul><li>Reverse reverses the elements in a collection. </li></ul></ul></ul><ul><ul><ul><li>In LINQ query expression, you can add several values separated by comma to the orderby expression. It takes the OrderBy() for first element and ThenBy() for the elements that follows. </li></ul></ul></ul><ul><li>var myStudents = from s in students where s.Marks >80 orderby s.Marks , s.Age select s; </li></ul>
    14. 14. Standard Query Operators in LINQ <ul><li>JOIN Related Operators: Join, GroupJoin </li></ul><ul><ul><li>With the Join operator a join of two collections can be done. This is similar to JOIN in SQL. </li></ul></ul><ul><li>Grouping Operators: GroupBy </li></ul><ul><ul><li>This operator groups a collection with a common key. </li></ul></ul><ul><li>Quantifiers: Any, All, Contains </li></ul><ul><ul><li>Any operator defines if any element in the collection satisfies a predicate function. </li></ul></ul><ul><ul><li>All determines if all elements in the collection satisfy a predicate. </li></ul></ul><ul><ul><li>Contains checks whether specific element is in the collection. </li></ul></ul><ul><li>Partitioning Operators: Take, Skip, TakeWhile, SkipWhile . </li></ul><ul><ul><li>With these operators you will get a partial result. With Take , you have to specify the number of elements to take from the collection, whereas the Skip ignores the specified number of elements and takes remaining. </li></ul></ul><ul><ul><li>The TakeWhile/SkipWhile takes/skips the elements as long as a condition is true. </li></ul></ul>
    15. 15. Standard Query Operators in LINQ <ul><li>Set Operators: Distinct, Union, Intersect, Except </li></ul><ul><ul><li>All these operators returns a collection set. The Distinct operator removes duplications from a collection. The Union operator returns unique elements that appear in the given two collections. The Intersect return elements that appear in both collections. The Except returns elements that appear in just one collection. </li></ul></ul><ul><li>Element Operators : First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault, ElementAt, ElementAtDefault . </li></ul><ul><ul><li>All these operators just returns one element. </li></ul></ul><ul><ul><li>The First operator returns first element that satisfies the condition. </li></ul></ul><ul><ul><li>The FirstOrDefault is similar to First , but it returns a default value of the type if the element is not found. </li></ul></ul><ul><ul><li>Similarly, Last returns the last element that satisfies the condition. </li></ul></ul><ul><ul><li>With ElementAt operator you can query for specific position element to return. </li></ul></ul><ul><ul><li>The Single returns the only one element that satisfies the condition. If more then one element satisfies the condition an exception is thrown. </li></ul></ul><ul><li>Aggregate Operators: Count, Sum, Min, Max, Average, Aggregate </li></ul>
    16. 16. Standard Query Operators in LINQ <ul><li>Conversion Operators: ToArray, ToEnumerable, ToList, ToDictionary OfType<T> </li></ul><ul><ul><li>These operators converts the collections to an Array, IEnumerable, IList , etc., </li></ul></ul><ul><li>Generation Operators: Empty, Range, Repeat </li></ul><ul><ul><li>These operators returns a new collection. With the Empty operator the empty collection is returned. The Range operator returns sequence of numbers and Repeat returns a collection with one repeated value. </li></ul></ul><ul><li>Miscellaneous Operators: Concat, EqualAll </li></ul><ul><li>Custom Sequence Operators: Combine </li></ul><ul><li>Query Execution: Deferred, Immediate, QueryReuse </li></ul>
    17. 17. Extending LINQ <ul><li>In Visual Studio 2008 includes LINQ provider assemblies that enable the use of LINQ with </li></ul><ul><ul><li>.NET Framework Collections </li></ul></ul><ul><ul><li>SQL Server Databases </li></ul></ul><ul><ul><li>ADO.NET Datasets </li></ul></ul><ul><ul><li>XML Documents. </li></ul></ul><ul><li>LINQ can be extended to support potentially any kind of data store. LINQ provider are being written by third parties for many web services and other database implementations. </li></ul><ul><li>Microsoft’s LINQ extensions are: </li></ul><ul><ul><li>LINQ to SQL </li></ul></ul><ul><ul><li>LINQ to DataSet </li></ul></ul><ul><ul><li>LINQ to Entities </li></ul></ul><ul><ul><li>LINQ to XML </li></ul></ul>
    18. 18. LINQ Project <ul><li>You can create any project on .NET Framework version 3.5 supports LINQ. </li></ul><ul><li>To enable basic LINQ functionality in your application : </li></ul><ul><ul><li>Just reference to System.Core.dll. </li></ul></ul><ul><ul><li>Add using directive statement for System.Linq to your source code file or project. </li></ul></ul><ul><ul><li>To enable advanced LINQ functionality such as expression trees you need add using directive statement for System.Linq.Expressions . </li></ul></ul><ul><ul><li>To enable LINQ to XML in your file additionally add using directive for System.Xml.Linq . </li></ul></ul><ul><ul><li>To enable LINQ to SQL in your file </li></ul></ul><ul><ul><ul><li>Add reference to System.Data.Linq.dll & System.Data.dll </li></ul></ul></ul><ul><ul><ul><li>Add the System.Data.Linq namespace reference with using directive. </li></ul></ul></ul><ul><ul><li>To enable LINQ to DataSet in your file additionally </li></ul></ul><ul><ul><ul><li>Add reference to System.Data.DataSetExtensions.dll & System.Data.dll </li></ul></ul></ul><ul><ul><ul><li>Add the System.Data.Linq namespace reference. </li></ul></ul></ul>
    19. 19. Visual Studio IDE Support for LINQ. <ul><li>The Visual Studio 2008 IDE provides the following features that support LINQ application development: </li></ul><ul><ul><li>Object Relational Designer : </li></ul></ul><ul><ul><ul><li>It is a visual design tool that you can use in LINQ to SQL applications to generate classes in C# or VB that represent the relational data in an underlying database. </li></ul></ul></ul><ul><ul><li>SQLMetal Command Line Tool: </li></ul></ul><ul><ul><ul><li>It is a command-line tool that can be used in build processes to generate classes from existing databases for use in LINQ to SQL applications </li></ul></ul></ul><ul><ul><li>LINQ-Aware Code Editors : </li></ul></ul><ul><ul><ul><li>Both the C# and Visual Basic code editors support LINQ extensively with new IntelliSense and formatting capabilities. </li></ul></ul></ul><ul><ul><li>Debugger Support: </li></ul></ul><ul><ul><ul><li>The Visual Studio debugger supports debugging of query expressions. </li></ul></ul></ul>
    20. 20. Thank You !