Showing posts with label Anonymous Types. Show all posts
Showing posts with label Anonymous Types. Show all posts

Monday, May 27, 2013

Anonymous Types

Anonymous types allow us to define variables and classes dynamically at runtime without having to declare them beforehand. The following example creates a string variable MyName dynamically at runtime without any specific declaration.

    var MyName = "Test Name";
    Console.WriteLine(MyName.GetType());

When this code executes the output will be as follows

Friday, June 8, 2012

Anonymous Types in LINQ


In this post Anonymous Types in LINQ, we shall see how Anonymous Types are used to capture output of LINQ expressions.

To know more about Anonymous Types refer the Post Anonymous Types in .Net 3.5

One of the main uses of Anonymous Types is to capture the output of LINQ expressions, the type and structure returned by a LINQ expressions will vary based on the Expression, it will be difficult to pre-define Types to hold the results returned by LINQ expressions,

Anonymous Types comes in handy in this situation, it accepts any type of result returned by the LINQ expressions without having to pre-define them.

In the below example we use LINQ to SQL to get the details of Employees, the results of the LINQ expression is captured in an Anonymous Type variable emp.

To know more about LINQ refer the Post What is LINQ?
To know more about LINQ to SQL refer the Post LINQ to SQL

Example:

EmployeeClassesDataContext
 dbContext = newEmployeeClassesDataContext();

var emp = (from e in dbContext.Employees
            select new { e.ID, e.Name, e.Phone });

grdEmployees.DataSource = emp;
grdEmployees.DataBind();

Notice that we are binding the Anonymous Type emp directly to the GridView grdEmployees, before executing the LINQ Query the type of emp is unknown, once the Query is executed, the Type emp stores the details of the Employees returned by the LINQ query and the same is bound to the GridView.

That’s it we have seen the usage of Anonymous Types in capturing results returned by LINQ Expressions

Related Posts

Anonymous Types in .Net 3.5




Anonymous Types were introduced in .Net 3.5, as the name suggests these types are not per-defined, their type is determined at runtime based on the value which is assigned to it.


The Anonymous Types derive directly from the Object, which makes it flexible enough to accommodate any type assigned to it at runtime.

Anonymous Types 
are created using the var keyword as follows,
var = Any Type

Example

var x = "Hello World"; //Here x Behaves like a String
var x = 10;            //Here x Behaves like an Integer
var x = true;          //Here x Behaves like a Boolean
var x = new list<Employee> ();
//Here x Behaves like a List of type Employee

Anonymous Types, support
IntelliSense, the IntelliSense context menu of type gets populated dynamically based on the value assigned to the type.




Apart from these simple types, Anonymous Types also support creating of complex objects dynamically as follows














Notice that once the Type is created Name and Age have got added to the list of Properties of the new Type x, all this happens dynamically.


Anonymous Types are extensively used in capturing the output of LINQ expressions, to know more on the usage of Anonymous Types in LINQ refer the post Anonymous Types in LINQ.


To know more about LINQ refer the post What is LINQ?

That's it we, have seen the usage of 
Anonymous Types in .Net 3.5





Related Post