Friday, January 15, 2010

Linq - How awesome can it get?

Hi All. I wanted to start a spot with a focus on Linq queries and/or general awesome use (and quite possibly awesome overkill) of Linq.

To start it off, a couple of us needed a month drop down list and decided to go to overkill land and came up with this:

using System.Globalization;
public static ListItem[] MonthNames
{
    get
    {
        int count = 1;
        return (from m in CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.Where(
s => !string.IsNullOrEmpty(s))
            select new ListItem()
            {
                Value = count.ToString(),
                Text = m,
                Selected = DateTime.Now.Month == count++
            }).ToArray();
    }
}

We wanted to integrate the count into the linq but didn't quite make it. You might also ask why we did the IsNullOrEmpty? Well, in the CultureInfo MonthNames there are 13 months (for places like Ethiopia (if I remember correctly) but it is empty in our locale.

This query builds ListItems with Values that are the numerical month and Text that is the month names, and the current month is selected.

I defy you to come up with something more awesome!

No comments:

Post a Comment