Linq Anonymous Types

Linq Anonymous Types

In Linq we can use anonymous types as shown in below example. Compiler generates type name and it is available at runtime instead of design type.

Example

var customer = new { FirstName=”Satalaj” , LastName = “More”, ProductCode = “Nine145” };

Console.WriteLine( “First Name = {0} : Last Name : {1}”, customer.FirstName, customer.LastName );

Usage

It is widely used in select clause.

Example

var productsQuery = 
    from p in products
    select new { p.Rate, prod.Size, p.Description };

foreach (var o in productsQuery)
{
    Console.WriteLine("Rate={0}, Size={1}, Description= {2}", o.Rate, o.Size, o.Description);
}

Tags:

Linq Group By Multiple ColumnsLinq Split String To ListLinq Inner JoinLearn Linq Step By StepHow to select multiple columns in Linq- C#.net VB.net codeLinq Distinct ListLinq Get Duplicate Count Of Words From StringLinq Union and intersect ExampleC# ArrayList Search using Linq Extension OfType MethodLinq ToDictionary ExtensionLinq to Sql

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.