Click here to Skip to main content
Click here to Skip to main content

Learning LINQ and Introduction to Important C# Features (All in One)

By , 16 Sep 2013
Rate this:
Please Sign up or sign in to vote.

Table of Contents 

  • I. Introduction
  • II. Pre-requisites
  • III. Overview
  • IV. What is Linq?
  • V. How Linq works internally
  • VI. Relational model vs. Hierarchical/graph model
    • a. Type declarations with simple relationships
    • b. Type declarations with two-way relationships
    • c. Querying the graph of objects
    • d. Database update calling the submitchanges() method
  • VII. XML manipulation
    • a. Reading the XML file of orders using an XMLReader
    • b. An XQuery like the following one to select nodes
    • c. Reading the XML file using Linq to XML
  • VIII. Language integration
  • IX. Type checking
  • X. Transparency across different type systems
  • XI. Linq flavors
  • XII. Linq to objects
  • XIII. Linq to ADO.NET
  • XIV. Linq to XML
  • XV. C# 2 and C# 3 important language features
    • 1. C# 2.0 revisited – Generics
    • 2. C# 2.0 revisited – Delegates
    • 3. C# 2.0 revisited – Anonymous Methods
    • 4. C# 2.0 revisited – Enumerators and Yield
      • a. IEnumerator and IEnumerable declarations
      • b. Multiple yield statements
      • c. Enumeration using yield (typed)
    • 5. C# 3.0 features – local type inference
    • 6. C# 3.0 features – lambda expressions
    • 7. C# 3.0 features – extension methods
      • a. Extension methods declaration
      • b. Extension methods for native value types
      • c. Extension methods resolution
      • d. Lambda expression as predicate
    • 8. C# 3.0 features – object initialization expressions
      • a. Explicit constructor call in object initializer
      • b. Nested object initializers
      • c. Initializers for owned objects
      • d. Collection initializers
    • 9. C# 3.0 features – Anonymous types
    • 10. C# 3.0 features – Query expressions
  • XVI. Linq syntax fundamentals
    • 1. Linq query expression
  • XVII. Conclusion

I. Introduction

This article explains some of the main and important concepts of LINQ and C#. The article explains LINQ in detail, and covers almost every concept of LINQ and C# 2.0 and 3.0. This is an effort to put together the important concepts in one place to make it easy to follow. I will explain the topics by giving simple examples, rather than explaining too much theory behind the examples.

II. Pre-requisites

  • The reader of this article should be aware of the LINQ term.
  • The reader should have basic knowledge of SQL.
  • The reader should know how to code in C#.

III. Overview

LINQ is so dynamic that it could be described in several ways as:

  • LINQ is a uniform programming model for any kind of data. LINQ enables you to query and manipulate data with a consistent model that is independent from data sources.
  • LINQ is just another tool for embedding SQL queries into code.
  • LINQ is yet another data abstraction layer.

IV. What Is LINQ?

In my terms, LINQ is actually a methodology that simplifies and unifies the implementation of any kind of data access. LINQ does not force you to use a specific architecture; it facilitates the implementation of several existing architectures for accessing data.

LINQ is a programming model that introduces queries as a first-class concept into any Microsoft .NET language. However, complete support for LINQ requires some extensions in the language used.

These extensions boost productivity, thereby providing a shorter, meaningful, and expressive syntax to manipulate data.

Examining a simple LINQ query:

var query = from c in Customers
                     where c.Country == "Italy"
                     select c.CompanyName; 

The SQL-like syntax used in LINQ is called a query expression, but remember LINQ is not the same as embedded SQL.

V. How LINQ Works Internally

When we look at the following query expression:

Customer[] Customers = GetCustomers();
var query = from c in Customers where c.Country == "Italy“ select c;

When the compiler compiles the above code, it generates the following code:

Customer[] Customers = GetCustomers();
IEnumerable<Customer> query =
Customers .Where( c => c.Country == "Italy" );

Look at another query expression:

var query = from c in Customers
where c.Country == "Italy“ orderby c.Name
select new { c.Name, c.City };

and the code when compiled:

var query = Customers.Where( c => c.Country == "Italy" ).OrderBy( c => c.Name ).Select
( c => new { c.Name, c.City } ); 

Therefore, we see the compiler internally treats this SQL type query as a simple C# lambda expressions query.

One important thing about LINQ to keep in mind is that a LINQ query is not really executed until there is access to the query result, this is also called on demand loading or lazy loading,

Example:

1.	var query = from c in Customers ...
      foreach ( string name in query ) ...

2.	var query = from c in Customers ...
      List<Customer> customers = query.ToList();

In both the above mentioned examples, the LINQ query does not execute in the first line actually, it is executed in the second line when that query variable is actually addressed or used. Write a code and try it yourself.

VI. Relational Model vs. Hierarchical/Graph Model

LINQ works on relational and hierarchical model. Rather than explaining theory, I would explain the concept with these examples:

  • In a relational model:
    var query = from c in Customers join o in Orders on c.CustomerID equals o.CustomerID
    select new {  c.CustomerID, c.CompanyName, o.OrderID };
  • In a hierarchical model:
    var query = from c in Customers from o in c.Orders
    select new {  c.Name, o.Quantity, o.Product. ProductName  };

The above two statements clearly state both the models, if you are aware of SQL queries, you can get the above given examples.

Follow the below mentioned scenarios.

A. Type declarations with simple relationships

class Customer
{
 public string Name;
 public string City;

 public Order[] Orders;
}

public class Order
{
 public int Quantity;
public Product Product;
}

public class Product
{
 public int ProductId;
public decimal Price;
public string ProductName;
}

B. Type declarations with two-way relationships

public class Customer
{
public string Name;
public string City;
public Order[] Orders;
}

public class Order
{
public int Quantity;
public Product Product;
public Customer Customer;
}

public class Product
{
public int ProductId;
public decimal Price;
public string ProductName;
public Order[] Orders;
}

C. Querying the graph of objects

var query = from p in products where	p.ProductId == 3 from o in p.Orders
select o; 
  • If you have entity relationships in your data model, you can still use explicit relationships in a LINQ:
    var query = from c in Customers join s in Suppliers on c.City equals s.City
    select new { c.City, c.Name, SupplierName = s.Name };

    And something like the following will be returned:

    City=Torino Name=Marco SupplierName=Trucker
    City=Dallas Name=James SupplierName=FastDelivery
    City=Dallas Name=James SupplierName=Horizon
    City=Seattle Name=Frank SupplierName=WayFaster
  • LINQ can return a hierarchy or graph of objects for a SQL query that contains several entities with one or more one-to-many relationships:
    var query = from c in Customers join s in _
    Suppliers on c.City equals s.City into customerSuppliers
    select new { c.City, c.Name, customerSuppliers };

Here is how the hierarchized results might appear:

City=Torino Name=Marco customerSuppliers=...
customerSuppliers: Name=Trucker City=Torino
City=Dallas Name=James customerSuppliers=...
customerSuppliers: Name=FastDelivery City=Dallas
customerSuppliers: Name=Horizon City=Dallas
City=Seattle Name=Frank customerSuppliers=...
customerSuppliers: Name=WayFaster City=Seattle
  • LINQ requires to describe your data in terms of entities that are also types in the language.
  • A LINQ query, it is always a set of operations on instances of some classes.
  • These objects might be the real container of data, or they might be a simple description (in terms of metadata) of the external entity one is going to manipulate.
  • A query can be sent to a database through an SQL command only if it is applied to a set of types that map tables and relationships contained in the database.
  • The conversion of all these operations in SQL commands is the responsibility of the LINQ engine.
  • Class declaration mapped on a database table:
[Table("Products")]
public class Product
{

[Column(IsPrimaryKey=true)]
public int IdProduct;
[Column(Name="UnitPrice")]
public decimal Price;
[Column()]
public string ProductName;

[Column()]
public bool Taxable;
[Column()]
public decimal Tax;
}

D. Database update calling the SubmitChanges() method

Example:

var taxableProducts = from p in db.Products where p.Taxable == true select p;

            foreach( Product product in taxableProducts ) { RecalculateTaxes( product ); }
           db.SubmitChanges();

VII. XML Manipulation

LINQ has a different set of classes and extensions to support the manipulation of XML data.

e.g. A fragment of an XML file of orders:

<?xml version="1.0" encoding="utf-8" ?>
<orders xmlns="http://schemas.devleap.com/Orders">
<order idCustomer="ALFKI" idProduct="1" quantity="10" price="20.59"/>
<order idCustomer="ANATR" idProduct="5" quantity="20" price="12.99"/>
<order idCustomer="KOENE" idProduct="7" quantity="15" price="35.50"/>
 </orders>

A. Reading the XML file of orders using an XmlReader

String nsUri = "http://schemas.devleap.com/Orders";
XmlReader xmlOrders = XmlReader.Create( "Orders.xml" );
List<Order> orders = new List<Order>();

Order order = null;

while (xmlOrders.Read())
   {

                    switch (xmlOrders.NodeType)
                        {
                        case XmlNodeType.Element:

if ((xmlOrders.Name == "order") && (xmlOrders.NamespaceURI == nsUri))
 {

                                      order = new Order();
                                      order.CustomerID = xmlOrders.GetAttribute( "idCustomer" );

                                      order.Product = new Product();
                                      order.Product.IdProduct = Int32.Parse
                                      ( xmlOrders.GetAttribute( "idProduct" ) );
                                      order.Product.Price = Decimal.Parse
                                      ( xmlOrders.GetAttribute( "price" ) );
                                      order.Quantity = Int32.Parse
                                      ( xmlOrders.GetAttribute( "quantity" ) );
                                      orders.Add( order );
                                    }
                        break;
                      }
                 }

B. An XQuery like the following one to select nodes

for $order in document("Orders.xml")/orders/order return $order 

C. Reading the XML file using LINQ to XML

XDocument xmlOrders = XDocument.Load( "Orders.xml" );
XNamespace ns = "http://schemas.devleap.com/Orders";
var orders = from o in xmlOrders.Root.Elements( ns + "order" )
select new Order {
CustomerID = (String)o.Attribute( "idCustomer" ),
Product = new Product {
IdProduct = (Int32)o.Attribute("idProduct"), Price = (Decimal)o.Attribute("price")
},
Quantity = (Int32)o.Attribute("quantity")
};

VIII. Language Integration

Language integration is a fundamental aspect of LINQ. It allows you to write code such as the following:

var query =
from c in Customers
where c.Country == "Italy" orderby c.Name
select new { c.Name, c.City };

Instead of writing this code:

var query = Customers
.Where( c => c.Country == "Italy" )
.OrderBy( c => c.Name )
.Select( c => new { c.Name, c.City } );
  • LINQ enables a more declarative style of coding for C# and Visual Basic.
  • In SQL, we describe what you want. In C#, we describe how to obtain the expected result.
  • Declarative programming can take advantage of services offered by compilers and frameworks, and in general, it is easier to read and maintain.
  • This single “feature” can be the most important one because it boosts programmers’ productivity.

IX. Type Checking

Another important aspect of language integration is type checking. Whenever data is manipulated by LINQ, no unsafe cast is necessary. Data is always strongly typed, including both the queried collections and the single entities that are read and returned.

This enables the use of Visual Studio features such as IntelliSense and Refactoring, even with LINQ queries.

X. Transparency Across Different Type Systems

The type system of the Microsoft .NET Framework and the type system of SQL Server are different. Using LINQ, we give precedence to the .NET type system because it is the one supported by any language that hosts a LINQ query. It is necessary to convert many types of data between these two worlds. LINQ handles this conversion for you automatically, making the differences in type systems almost completely transparent to the programmer.

XI. LINQ Flavors

LINQ is a technology that covers many data domains. Some of these domains are included in those “LINQ Flavors” that Microsoft provides as part of the .NET 3.5 Framework, as shown below:

XII. LINQ to Objects

LINQ to Objects has the goal of manipulating collections of objects, which can be related to each other to form a hierarchy or a graph. From a certain point of view, LINQ to Objects is the default implementation used by a LINQ query. LINQ to Objects is enabled including the System.Linq namespace.

XIII. LINQ to ADO.NET

LINQ to ADO.NET includes different LINQ implementations that share the need to manipulate relational data. It includes other technologies that are specific to each particular persistence layer:

  • LINQ to SQL

    Handles the mapping between custom types in C# and the physical table schema

  • LINQ to Entities

    Is in many ways similar to LINQ to SQL. However, instead of using the physical database as a persistence layer, it uses a conceptual Entity Data Model (EDM). The result is an abstraction layer that is independent from the physical data layer.

  • LINQ to DataSet

    Makes it possible to query a DataSet using LINQ

XIV. LINQ to XML

LINQ to XML offers a slightly different syntax that operates on XML data, allowing query and data manipulation.This query corresponds to the following C# 3.0 syntax:

var book =
new XElement( "Book",
new XAttribute( "Title", "Introducing LINQ" ), from person in team
where person.Role == "Author“ select new XElement( "Author", person.Name ) );

XV. C#2 and C#3 Important Language Features

Original content of this article:http://language-integrated-query.com/Linq_CSharp_3.0_Features.aspx  

1. C# 2.0 Revisited – Generics

You must be aware of this concept very well, but I wanted to cover this in my way taking simple examples.

Let’s take Min() function of C# for example,

Problem

The Min function, following is the issue:

int Min( int a, int b )
{ if (a < b) return a; else return b; }
object Min( object a, object b )
{ if (a < b) return a; else return b; }
IComparable Min( IComparable a, IComparable b )
{ if (a.CompareTo( b ) < 0) return a; else return b; }
int a = 5, b = 10;
int c = (int) Min( a, b ); 

I hope you got the issue, yes type casting.

C# 2.0 solved this problem with generics. The basic principle of generics is that type resolution is moved from the C# compiler to the jitter. Here is the generic version of the Min function:

Solution

T Min<T>( T a, T b ) where T : IComparable<T> {
                           if (a.CompareTo( b ) < 0) return a;
    else return b;
}

2. C# 2.0 Revisited – Delegates

A delegate is a class that encapsulates one or more method. Internally, one delegate stores a list of method pointers, each of which can be paired with a reference to an instance of the class containing an instance method.

A delegate can be declared as:

delegate void SimpleDelegate(); delegate int ReturnValueDelegate();
delegate void TwoParamsDelegate( string name, int age ); 
  • In C# 1.X, Delegates were instantiated as:
    public class DemoDelegate { void MethodA() { … }
    int MethodB() { … }
    
    void MethodC( string x, int y ) { … }
    
    void CreateInstance() {
    
    SimpleDelegate a = new SimpleDelegate( MethodA );
    ReturnValueDelegate b = new ReturnValueDelegate ( MethodB );
    
    TwoParamsDelegate c = new TwoParamsDelegate(
    
    MethodC ); // … } }
  • Delegate instantiation in C# 2.0:
    public class DemoDelegate
     {
    void MethodA() { … }
    
    int MethodB() { … }
    
    void MethodC( string x, int y ) { … }
    
               void CreateInstance() {
                     SimpleDelegate a = MethodA;
                      ReturnValueDelegate b = MethodB;
                     TwoParamsDelegate c = MethodC; //              }
    
     // … }

3. C# 2.0 Revisited – Anonymous Methods

  • Using an anonymous method:
    public class DemoDelegate {
    void Repeat10Times( SimpleDelegate someWork )
    { for (int i = 0; i < 10; i++) someWork(); }
    void Run2() {
    int counter = 0;
    this.Repeat10Times( delegate { Console.WriteLine("C# chapter" );
    counter++; } );
    Console.WriteLine( counter ); } // … }
  • Parameters for an anonymous method:
    public class DemoDelegate {
    
    void Repeat10Times( TwoParamsDelegate callback )
     { for (int i = 0; i < 10; i++)
    
    callback( "Linq book", i ); }
    
    void Run3() { Repeat10Times(
    
    delegate( string text, int age )
    
    { Console.WriteLine( "{0} {1}", text, age ); } );
    
    }
    
    // … }

4. C# 2.0 Revisited – Enumerators and Yield

a. IEnumerator and IEnumerable declarations

public interface IEnumerator
    {
bool MoveNext();
object Current { get; }
void Reset();
    }

public interface IEnumerable
    {
           IEnumerator GetEnumerator();
    }

C# 2.0 introduced the yield statement through which the compiler automatically generates a class that implements the IEnumerator interface returned by the GetEnumerator method. The yield statement can be used only immediately before a return or break keyword. Enumeration using a yield statement:

public class CountdownYield : IEnumerable {
public int StartCountdown;

public IEnumerator GetEnumerator() {
  for (int i = StartCountdown - 1; i >= 0; i--)
   {
     yield return i; }
   }
}

b. Multiple yield statements

             public class CountdownYieldMultiple : IEnumerable
               {
                     public IEnumerator GetEnumerator() {

             yield return 4;
             yield return 3;
             yield return 2;
             yield return 1;
             yield return 0;

         }
   }

c. Enumeration using yield (typed)

public class CountdownYieldTypeSafe : IEnumerable<int> {
public int StartCountdown;

                      IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); }

public IEnumerator<int> GetEnumerator() {

  for (int i = StartCountdown - 1; i >= 0; i--)

   { yield return i; }
}
}

5. C# 3.0 Features – Local Type Inference

C# 3.0 offers type inference that allows us to define a variable by using the var keyword instead of a specific type. This might seem to be equivalent to defining a variable of type object, but it is not.

     var a = 2; //a is declared as int
     object b = 2; // Boxing an int into an object
     int C = a; // No cast, no unboxing
     int D = (int)b // Cast is required, an unboxing is done
  • When var is used, the compiler infers the type from the expression used to initialize the variable. The compiled IL code contains only the inferred type.
  • In other words, consider this code:
      int a = 5;
      var b = a;
  • It is perfectly equivalent to this example:
      int a = 5;
      int b = a; 
  • The var keyword can be used only within a local scope.
  • In other words, a local variable can be defined in this way, but not a member or a parameter.
  • The following code shows some examples of valid uses of var:
    public void ValidUse( decimal d )
      {
        var x = 2.3; // double
    
        var y = x; // double
        var r = x / y; // double
    
        var s = "sample"; // string
        var l = s.Length; // int
        var w = d; // decimal
    
        var p = default(string); // string
    }
  • The sample shows some cases in which the var keyword is not allowed:
    class VarDemo {
    
    // invalid token 'var' in class, struct or interface member declaration
    
    var k =0;
    
    // type expected in parameter list
    
    public void InvalidUseParameter( var x ){} // type expected in result type declaration
    public var InvalidUseResult() { return 2; }
    
    public void InvalidUseLocal() {
    
    var x; // Syntax error, '=' expected
    
    var y = null; // Cannot infer local variable type from 'null'
    }
                // … }

6. C# 3.0 Features – Lambda Expressions

C# 2.0 introduced the capability to “pass a pointer to some code” as a parameter by using anonymous methods. This concept is a powerful one, but what you really pass in this way is a reference to a method, not exactly a piece of code. That reference points to strongly typed code that is generated at compile time. Using generics, we can obtain more flexibility, but it is hard to apply standard operators to a generic type.

C# 3.0 introduces lambda expressions, which allow the definition of anonymous methods using more concise syntax. Lambda expressions can also optionally postpone code generation by creating an expression tree that allows further manipulation before code is actually generated, which happens at execution time.

Lambda expression examples:

•	( int a, int b ) => { return a + b; } // Explicitly typed, statement body
•	( int a, int b ) => a + b; // Explicitly typed, expression body

•	( a, b ) => { return a + b; } // Implicitly typed, statement body
•	( a, b ) => a + b // Implicitly typed, expression body

•	( x ) => sum += x // Single parameter with parentheses
•	x => sum += x // Single parameter no parentheses

•	() => sum + 1 // No parameters

Some lambda expressions have a particular name based on their purpose. A predicate is a Boolean expression that is intended to indicate membership of an element in a group. For example, it is used to define how to filter items inside a loop:

// Predicate

 age ) => age > 21

A projection is an expression that returns a type different from the type of its single parameter:

// Projection: takes a string and returns an int
 ( s ) => s.Length
  • Lambda expression as a predicate:
    public static void Demo() {
    string[] names = { "Marco", "Paolo", "Tom" }; 
    Display( names, s => s.Length > 4 ); }
    public static void Display<T>( T[] names, Func<T, bool> filter ){
    foreach( T s in names) {
    if (filter( s )) Console.WriteLine( s ); }
    }
  • A lambda expression can also be assigned to a variable of these delegate types:
    public delegate T Func<T>();
    public delegate T Func<A0, T>( A0 arg0 );
    public delegate T Func<A0, A1, T>( A0 arg0, A1 arg1 );
    public delegate T Func<A0, A1, A2, T>( A0 arg0, A1 arg1, A2 arg2 );
    public delegate T Func<A0, A1, A2, A3, T>( A0 arg0, A1 arg1, A2 arg2, A3 arg3 );

7. C# 3.0 Features – Extension Methods

C# 3.0 introduces a syntax that conceptually extends an existing type (either reference or value) by adding new methods without deriving it into a new type. The methods that extend a type can use only the public members of the type itself, just as you can do from any piece of code outside the target type.

An extension method must be static and public, must be declared inside a static class, and must have the keyword this before the first parameter type, which is the type that the method extends.

Extension methods are public because they can be (and normally are) called from outside the class where they are declared.

However, the result type of the extension method might be the extended type itself.

LINQ very frequently uses extension methods in this way.

The following code shows a traditional approach to writing two methods (FormattedUS and FormattedIT) that converts a decimal value into a string formatted with a specific culture:

static class Traditional {
public static void Demo() {
decimal x = 1234.568M;
Console.WriteLine( FormattedUS( x ) );

Console.WriteLine( FormattedIT( x ) );
}

public static string FormattedUS( decimal d ) {
           return String.Format( formatIT, "{0:#,0.00}", d );
}
public static string FormattedIT( decimal d ) {
           return String.Format( formatUS, "{0:#,0.00}", d );
}

     static CultureInfo formatUS = new CultureInfo( "en-US" );
     static CultureInfo formatIT = new CultureInfo( "it-IT" );
}

a. Extension methods declaration

using System;
using System.Globalization;

internal static class ExtensionMethods
{
    public static void Demo()
    {
        decimal x = 1234.568M;
        Console.WriteLine(x.FormattedUS());
        Console.WriteLine(x.FormattedIT());
        Console.WriteLine(FormattedUS(x)); // Traditional call allowed
        Console.WriteLine(FormattedIT(x));// Traditional call allowed
    }

    private static CultureInfo formatUS = new CultureInfo("en-US");
    private static CultureInfo formatIT = new CultureInfo("it-IT");

    public static string FormattedUS(this decimal d)
    {
        return String.Format(formatIT, "{0:#,0.00}", d);
    }

    public static string FormattedIT(this decimal d)
    {
        return String.Format(formatUS, "{0:#,0.00}", d);
    }
}

b. Extension methods for native value types

static class ExtensionMethods {
public static decimal Double( this decimal d ) { return d + d; }
public static decimal Triple( this decimal d ) { return d * 3; }
public static decimal Increase( this decimal d ) { return ++d; }
public static decimal Decrease( this decimal d ) { return --d; }
public static decimal Half( this decimal d ) { return d / 2; } //}

Extension methods call order:

decimal	x=14M,y= 14M;
x =	Half(Triple(Decrease(Decrease(Double(Increase(x)	))))	);
y = y.Increase().Double().Decrease().Decrease().Triple().Half( ); 
  • An extension method is not automatically considered. Its resolution follows some rules.
  • Here is the order of evaluation used to resolve a method for an identifier.
  • Instance method: If an instance method exists, it has priority.
  • Extension method: The search for an extension method is made through all static classes in the “current namespace” and in all namespaces included in active using directives.

c. Extension methods resolution

public class A
{
    public virtual void X() { }
}
public class B : A
{
    public override void X() { }
    public void Y() { }
}
static public class E
{
    static void X(this A a) { }
    static void Y(this A b) { }
    public static void Demo() {
A a = new A();
B b = new B();
A c = new B();
a.X(); // Call	A.X
b.X(); // Call	B.X
c.X(); // Call	B.X
a.Y(); // Call	E.Y
b.Y(); // Call	B.Y
c.Y(); // Call E.Y }
}

d. Lambda expression as predicate

public static void Display<T>( this T[] names, Func<T, bool> filter ) {…}

public static void Demo() {

string[] names = { "Marco", "Paolo", "Tom" };
names.Display( s => s.Length > 4 );// It was: Display( names, s => s.Length > 4 );

}

8. C# 3.0 Features – Object Initialization Expressions

C# 3.0 introduces a shorter form of object initialization syntax that generates functionally equivalent code:

// Implicitly calls default constructor before object initialization
Customer customer = new Customer { Name = "Marco", Country = "Italy" };

a. Explicit constructor call in object initializer

// Explicitly specify constructor to call before object initialization
Customer c1 = new Customer() { Name = "Marco", Country = "Italy" };
// Explicitly specify nondefault constructor
Customer c2 = new Customer( "Paolo", 21 ) { Country = "Italy" };

b. Nested object initializers

public class Point
{
    private int x, y;

    public int X
    {
        get { return x; }
        set { x = value; }
    }

    public int Y
    {
        get { return y; }
        set { y = value; }
    }
}

public class Rectangle
{
    private Point tl, br;

    public Point TL
    {
        get { return tl; }
        set { tl = value; }
    }

    public Point BR
    {
        get { return br; }
        set { br = value; }
    }
}

// Possible code inside a method
Rectangle r = new Rectangle { TL = new Point { X = 0, Y = 1 },BR = new Point { X = 2, Y = 3 } };

c. Initializers for owned objects

public class Rectangle { Point tl = new Point(); Point br = new Point();

public Point TL { get { return tl; } } public Point BR { get { return br; } } }

// Possible code inside a method
Rectangle r = new Rectangle { TL = { X = 0, Y = 1 },BR = { X = 2, Y = 3 } };

d. Collection initializers

//Collection classes that implement ICollection<T>
List<int> integers = new List<int> { 1, 3, 9, 18 };
List<Customer> list = new List<Customer> {

new Customer( "Jack", 28 ) { Country = "USA"}, new Customer { Name = "Paolo" },

new Customer { Name = "Marco", Country = "Italy" }
};

//Collection classes that implement IEnumerable
ArrayList integers = new ArrayList() { 1, 3, 9, 18 };
ArrayList list = new ArrayList {

new Customer( "Jack", 28 ) { Country = "USA"}, new Customer { Name = "Paolo" },

new Customer { Name = "Marco", Country = "Italy" }
 }; 

9. C# 3.0 Features – Anonymous Types

An object initializer can also be used without specifying the class that will be created with the new operator.

Doing that, a new class-an anonymous type-is created.

Consider the example shown below:

Customer c1 = new Customer { Name = "Marco" };
 var c2 = new Customer { Name = "Paolo" };

var c3 = new { Name = "Tom", Age = 31 };
var c4 = new { c2.Name, c2.Age };

var c5 = new { c1.Name, c1.Country };
var c6 = new { c1.Country, c1.Name };

When you do GetType() - the following is the output that is generated:

c1 is Customer c2 is Customer c3 is:

<>f__AnonymousType0`2[System.String,System.Int32] c4 is
<>f__AnonymousType0`2[System.String,System.Int32] c5 is
 <>f__AnonymousType5`2[System.String,System.String] c6 is
<>f__AnonymousTypea`2[System.String,System.String]

10. C# 3.0 Features – Query Expressions

C# 3.0 also introduces query expressions, which have a syntax similar to the SQL language and are used to manipulate data. This syntax is converted into regular C# 3.0 syntax that makes use of specific classes, methods, and interfaces that are part of the LINQ libraries.

XVI. LINQ Syntax Fundamentals

1. LINQ Query Expression

The following code shows a prototype of the full syntax of a LINQ query expression:

query-expression ::=
from-clause query-body query-body
::= join-clause* (from-clause join-clause* | let-clause | where-clause)*
orderby-clause? (select-clause | groupby-clause) query-continuation?
from-clause ::= from itemName in srcExpr _
select-clause ::= select selExpr groupby-clause ::= group selExpr by keyExpr

The first from clause can be followed by zero or more from, let, or where clauses.

A let clause applies a name to the result of an expression, while a where clause defines a filter that will be applied to include specific items in the results. Each from clause is a generator that represents an iteration over a sequence on which query operators (such as the extension methods of System.Linq.Enumerable) are applied.

let-clause ::= let itemName = selExpr where-clause ::= where predExpr

A from clause can be followed by any number of join clauses. The final select or group clause can be preceded by an orderby clause that applies an ordering to the results:

join-clause ::= join itemName in srcExpr on keyExpr equals keyExpr (into itemName)?

orderby-clause ::= orderby (keyExpr (ascending | descending)?)* _
query-continuation ::= into itemName query-body.

XVII. Conclusion

Long post, I guess we covered a lot of concepts deeply. You can learn more theory from MSDN or other renowned community sites. We covered very important and deep concepts of LINQ, C#2.0 and C# 3.0.

Happy coding! 

Original content of this article:http://language-integrated-query.com/Linq_CSharp_3.0_Features.aspx    

To read more informative articles on C#, ASP.NET and MVC, follow A Practical Approach.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Akhil_Mittal
Software Developer (Senior)
India India
I am a C# Corner MVP,a Code project MVP,author,blogger and currently working as an Engineer Analyst in an MNC and have an experience of more than 6 years in C#.Net. I am a B.Tech in Computer Science and hold a diploma in Information Security and Application Development. My work experience includes Development of Enterprise Applications using C#,.Net and Sql Server,Analysis as well as Research and Development. I am a MCP in Web Applications(MCTS-70-528,MCTS-70-515) and .Net Framework 2.0 (MCTS-70-536). Please visit my blog A Practical Approach for more informative articles.
____________________________________________________________________________________________________________________
 
Article of the Day on Microsoft's site http://www.asp.net/community/articles on 16 August 2013.
Article of the Day on Microsoft's site http://www.asp.net/community/articles on 28 August 2013.
Article of the Day on Microsoft's site http://www.asp.net/community/articles on 08 Sept 2013.
Article of the Day on Microsoft's site http://www.asp.net/community/articles on 29 Sept 2013.
Article of the Day on Microsoft's site http://www.asp.net/community/articles on 29 Oct 2013.
Member of the month for July 2013 on C# Corner
Month Winner for July 2013 on C# Corner
http://www.c-sharpcorner.com/News/3067/july-2013-month-winners-announced.aspx
Group type: Collaborative Group

108 members

Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinmemberSouthmountain27-Sep-13 5:54 
GeneralRe: My vote of 5 PingroupAkhil_Mittal29-Sep-13 3:57 
QuestionA little out of date PinmemberClifford Nelson17-Sep-13 5:24 
AnswerRe: A little out of date PingroupAkhil_Mittal18-Sep-13 3:45 
GeneralMy vote of 5 PinprofessionalRenju Vinod16-Sep-13 20:24 
GeneralRe: My vote of 5 PingroupAkhil_Mittal16-Sep-13 20:26 
Thanks for reading.
Thanks
Do not forget to comment and rate the article if it helped you by any means Smile | :) ...
For any technical requirement related to .net ,OOPS,C# and design patterns contact me on akhil.mittal20@gmail.com

GeneralMy vote of 5 PinprofessionalKRPTL16-Sep-13 19:16 
GeneralRe: My vote of 5 PingroupAkhil_Mittal16-Sep-13 20:14 
GeneralMy vote of 5 PinmemberBalkrishna Patel16-Sep-13 19:11 
GeneralRe: My vote of 5 PingroupAkhil_Mittal16-Sep-13 20:14 
GeneralMy vote of 5 Pinmembergpst16-Sep-13 18:48 
GeneralMy vote of 5 Pinmembergpmsc16-Sep-13 18:47 
GeneralMy vote of 5 Pinmemberswas.agr16-Sep-13 6:30 
GeneralMy vote of 5 PinmemberChris Dis16-Sep-13 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140421.2 | Last Updated 16 Sep 2013
Article Copyright 2013 by Akhil_Mittal
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid