Write a code snippet for printing Month Names and Month number using LINQ

Posted by Nagasundar_Tn under LINQ category on | Points: 40 | Views : 1996
   static void Main(string[] args)
var AllMonths = Enumerable.Range(1, 12).Select(a => new
Name = System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(a).ToUpper(),
Code = a.ToString()

foreach (var month in AllMonths)
Console.WriteLine(string.Format("Month No {0} Month Name {1} ", month.Code, month.Name));

By using Linq we can get the month names in one line code. In the above code I used AllMoths variable. Enumerable.Range gives the number between the Range that we pass in parameters. Using GetMonthName function I got the month name and getting the number (a) as month number and I am printing in separate foreach loop.

Comments or Responses

Login to post response