select clause with anonymous type in LINQ



Declaring and using an anonymous type object
The keyword var is used to declare anonymous type objects. The new operator, along with an object initializer, is used to define the value.

using System;
using System.Linq;

namespace CSharpLINQ
{
    class SimpleQuery
    {
		static void Main( )
		{
			var students = new[] // Array of objects of an anonymous type
			{
				new { LName="Mishra", FName="Kuldeep", Age=31 },
				new { LName="Sharma", FName="Ramesh", Age=20 },
				new { LName="Verma", FName="Manoj", Age=21 }
			};

			var query = from student in students select new {student.FName, student.Age};
			
			foreach (var q in query)
				Console.WriteLine("Name: {0}, Age: {1}", q.FName, q.Age);
		}
    }
}

0 Comment's

Comment Form