Published on

Good to see like this

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

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

No notes for slide


  1. 1. Introduction to LINQ <ul><li>What is LINQ? </li></ul><ul><li>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. </li></ul>
  2. 2. Various Data sources and LINQ <ul><li>By using LINQ you can query and transform the data in </li></ul><ul><li>XML Document </li></ul><ul><li>SQL Databases </li></ul><ul><li>ADO.NET Datasets </li></ul><ul><li>.NET Collections </li></ul><ul><li>Objects </li></ul>
  3. 3. LINQ architecture [ Language-Integrated Query]
  4. 4. Operation Parts <ul><li>LINQ Query operation contains three parts </li></ul><ul><li>Obtain the data source. </li></ul><ul><li>Create the Query. </li></ul><ul><li>Execute the Query. </li></ul>
  5. 5. Discussion with Example
  6. 6. Explanation <ul><li>In the above example: </li></ul><ul><ul><ul><ul><ul><li>B. </li></ul></ul></ul></ul></ul>
  7. 7. <ul><li>a. We did not retrieve the entire data in query statement it self. </li></ul><ul><li>b. In the Foreach loop the query results will be retrieved. </li></ul><ul><li>As Array Implicitly supports IEnumerable<T> interface , it is queriable by LINQ. </li></ul>
  8. 8. Few more Interesting points <ul><li>LINQ - XML </li></ul><ul><li>To Query an XML file with LINQ we need to use a queryable  Xelement type. </li></ul>
  9. 9. LINQ - SQL <ul><li>With LINQ to SQL, you first create an object-relational mapping at design time either manually or by using the Object Relational Designer (O/R Designer). You write your queries against the objects, and at run-time LINQ to SQL handles the communication with the database </li></ul>
  10. 10. Must know!!! <ul><li>Anonymous Types: </li></ul><ul><li>Def: </li></ul><ul><li>Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. </li></ul><ul><li>Type [var] will be defined by the compiler at run time </li></ul><ul><li>Anonymous types are  class  types that consist of one or more public read-only properties. </li></ul><ul><li>No other kinds of class members such as methods or events are allowed. </li></ul>var v = new { Amount = 108, Message = &quot;Hello&quot; };
  11. 11. LINQ and Anonymous types <ul><li>Anonymous types are typically used in the select clause of a query expression to return a subset of the properties from each source object. </li></ul><ul><li>For CLR, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object. </li></ul><ul><li>The compiler gives them a name although your application cannot access it </li></ul><ul><li>Scope: It has method scope, if we need to pass AT to another method first we need to type cast it as object type then pass it. </li></ul>
  12. 12. Object and Collection Initializers <ul><li>Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor. </li></ul><ul><li>Implementation: </li></ul>
  13. 13. LINQ and Obj and Collection Initializers <ul><li>Query expressions make frequent use of anonymous types, which can only be initialized with an object initializers. </li></ul><ul><li>In the select clause, a query expression can transform objects of the original sequence into objects whose value and shape may differ from the original. This is very useful if you want to store only a part of the information in each object in a sequence. </li></ul><ul><li>Implementation: </li></ul>
  14. 14. Collection Initializers <ul><li>Collection initializers let you specify one or more element intializers when you initialize a collection class that implements IEnumerable. The element initializers can be a simple value, an expression or an object initializer. </li></ul><ul><li>  By using a collection initializer you do not have to specify multiple calls to the  Add  method of the class in your source code; the compiler adds the calls. </li></ul><ul><li>Example: </li></ul><ul><li>Adding elements to Object types: </li></ul>
  15. 15. <ul><li>Y ou can specify null as an element in a collection initializer if the collection's  Add  method allows it as below. </li></ul>
  16. 16. Consolidated Example <ul><li>class ObjInitializers </li></ul><ul><li>{ </li></ul><ul><li>private class Cat </li></ul><ul><li>{ </li></ul><ul><li>// Auto-implemented properties. </li></ul><ul><li>public int Age { get; set; } </li></ul><ul><li>public string Name { get; set; } </li></ul><ul><li>} </li></ul><ul><li>static void Main() </li></ul><ul><li>{ </li></ul><ul><li>Cat cat = new Cat { Age = 10, Name = &quot;Fluffy&quot; }; </li></ul><ul><li>List<Cat> cats = new List<Cat> </li></ul><ul><li>{ </li></ul><ul><li>new Cat(){ Name = &quot;Sylvester&quot;, Age=8 }, </li></ul><ul><li>new Cat(){ Name = &quot;Whiskers&quot;, Age=2 }, </li></ul><ul><li>new Cat(){ Name = &quot;Sasha&quot;, Age=14 } </li></ul><ul><li>}; </li></ul><ul><li>List<Cat> moreCats = new List<Cat> </li></ul><ul><li>{ </li></ul><ul><li>new Cat(){ Name = &quot;Furrytail&quot;, Age=5 }, </li></ul><ul><li>new Cat(){ Name = &quot;Peaches&quot;, Age=4 }, </li></ul><ul><li>null </li></ul><ul><li>}; </li></ul><ul><li>// Display results. </li></ul><ul><li>System.Console.WriteLine(cat.Name); </li></ul><ul><li>foreach (Cat c in cats) </li></ul><ul><li>System.Console.WriteLine(c.Name); </li></ul><ul><li>foreach (Cat c in moreCats) </li></ul><ul><li>if (c != null) </li></ul><ul><li>System.Console.WriteLine(c.Name); </li></ul><ul><li>else </li></ul><ul><li>System.Console.WriteLine(&quot;List element has null value.&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  17. 17. Partial Method <ul><li>A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type. </li></ul><ul><li>  If the developer does not supply an implementation, the compiler removes the signature at compile time. </li></ul><ul><li>The following conditions apply to partial methods: </li></ul><ul><li>Signatures in both parts of the partial type must match. </li></ul><ul><li>The method must return void. </li></ul><ul><li>No access modifiers or attributes are allowed. Partial methods are implicitly private. </li></ul>
  18. 18. Example on Partial Method