Introduction to LINQ What is LINQ? 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.
Various Data sources and LINQ By using LINQ you can query and transform the data in XML Document SQL Databases ADO.NET Datasets .NET Collections Objects
LINQ architecture [ Language-Integrated Query]
Operation Parts LINQ Query operation contains three parts Obtain the data source. Create the Query. Execute the Query.
Discussion with Example
Explanation  In the above example: B.
a. We did not retrieve the entire data in query statement it self. b. In the  Foreach   loop the query results will be retrieved.  As Array Implicitly supports IEnumerable<T> interface , it is queriable by LINQ.
Few more Interesting points LINQ - XML To Query an XML file with LINQ we need to use  a queryable   Xelement  type.
LINQ - SQL 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
Must know!!! Anonymous Types: Def: 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.  Type [var] will be defined by the compiler at run time Anonymous types are  class  types that consist of one or more public read-only properties. No other kinds of class members such as methods or events are allowed. var v = new { Amount = 108, Message = &quot;Hello&quot; };
LINQ and Anonymous types Anonymous types are typically used in the select clause of a query expression to return a subset of the properties from each source object. 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. The compiler gives them a name although your application cannot access it 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.
Object and Collection Initializers 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.  Implementation:
LINQ and Obj and Collection Initializers Query expressions make frequent use of anonymous types, which can only be initialized with an object initializers. 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. Implementation:
Collection Initializers 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.   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. Example: Adding elements to Object types:
Y ou can specify null as an element in a collection initializer if the collection's  Add  method allows it as below.
Consolidated Example  class ObjInitializers { private class Cat { // Auto-implemented properties. public int Age { get; set; } public string Name { get; set; } } static void Main() { Cat cat = new Cat { Age = 10, Name = &quot;Fluffy&quot; }; List<Cat> cats = new List<Cat> { new Cat(){ Name = &quot;Sylvester&quot;, Age=8 }, new Cat(){ Name = &quot;Whiskers&quot;, Age=2 }, new Cat(){ Name = &quot;Sasha&quot;, Age=14 } }; List<Cat> moreCats = new List<Cat> { new Cat(){ Name = &quot;Furrytail&quot;, Age=5 }, new Cat(){ Name = &quot;Peaches&quot;, Age=4 }, null }; // Display results. System.Console.WriteLine(cat.Name); foreach (Cat c in cats) System.Console.WriteLine(c.Name); foreach (Cat c in moreCats) if (c != null) System.Console.WriteLine(c.Name); else System.Console.WriteLine(&quot;List element has null value.&quot;); } }
Partial Method A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type.   If the developer does not supply an implementation, the compiler removes the signature at compile time. The following conditions apply to partial methods: Signatures in both parts of the partial type must match. The method must return void. No access modifiers or attributes are allowed. Partial methods are implicitly private.
Example on Partial Method

Linq

  • 1.
    Introduction to LINQWhat is LINQ? 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.
  • 2.
    Various Data sourcesand LINQ By using LINQ you can query and transform the data in XML Document SQL Databases ADO.NET Datasets .NET Collections Objects
  • 3.
    LINQ architecture [Language-Integrated Query]
  • 4.
    Operation Parts LINQQuery operation contains three parts Obtain the data source. Create the Query. Execute the Query.
  • 5.
  • 6.
    Explanation Inthe above example: B.
  • 7.
    a. We didnot retrieve the entire data in query statement it self. b. In the Foreach loop the query results will be retrieved. As Array Implicitly supports IEnumerable<T> interface , it is queriable by LINQ.
  • 8.
    Few more Interestingpoints LINQ - XML To Query an XML file with LINQ we need to use a queryable  Xelement type.
  • 9.
    LINQ - SQLWith 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
  • 10.
    Must know!!! AnonymousTypes: Def: 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. Type [var] will be defined by the compiler at run time Anonymous types are  class  types that consist of one or more public read-only properties. No other kinds of class members such as methods or events are allowed. var v = new { Amount = 108, Message = &quot;Hello&quot; };
  • 11.
    LINQ and Anonymoustypes Anonymous types are typically used in the select clause of a query expression to return a subset of the properties from each source object. 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. The compiler gives them a name although your application cannot access it 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.
  • 12.
    Object and CollectionInitializers 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. Implementation:
  • 13.
    LINQ and Objand Collection Initializers Query expressions make frequent use of anonymous types, which can only be initialized with an object initializers. 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. Implementation:
  • 14.
    Collection Initializers Collectioninitializers 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.   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. Example: Adding elements to Object types:
  • 15.
    Y ou canspecify null as an element in a collection initializer if the collection's  Add  method allows it as below.
  • 16.
    Consolidated Example class ObjInitializers { private class Cat { // Auto-implemented properties. public int Age { get; set; } public string Name { get; set; } } static void Main() { Cat cat = new Cat { Age = 10, Name = &quot;Fluffy&quot; }; List<Cat> cats = new List<Cat> { new Cat(){ Name = &quot;Sylvester&quot;, Age=8 }, new Cat(){ Name = &quot;Whiskers&quot;, Age=2 }, new Cat(){ Name = &quot;Sasha&quot;, Age=14 } }; List<Cat> moreCats = new List<Cat> { new Cat(){ Name = &quot;Furrytail&quot;, Age=5 }, new Cat(){ Name = &quot;Peaches&quot;, Age=4 }, null }; // Display results. System.Console.WriteLine(cat.Name); foreach (Cat c in cats) System.Console.WriteLine(c.Name); foreach (Cat c in moreCats) if (c != null) System.Console.WriteLine(c.Name); else System.Console.WriteLine(&quot;List element has null value.&quot;); } }
  • 17.
    Partial Method Apartial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type.   If the developer does not supply an implementation, the compiler removes the signature at compile time. The following conditions apply to partial methods: Signatures in both parts of the partial type must match. The method must return void. No access modifiers or attributes are allowed. Partial methods are implicitly private.
  • 18.