Tag Archives: linq

Why do I love Extension Methods in System.Linq ?

Because it helps me to write compact and clean code of course !

For example, I need to expose a property that gives the number of visible items in my ViewModel.

Old way:

        public int VisibleItemsCount
        {
            get
            {
                int i = 0;
                foreach (var item in this.Items)
                {
                    if (item.IsVisible)
                        i++;
                }
                return i;
            }
        }

Linq way:

public int VisibleItemsCount
{
    get
    {
        return this.Items.Count(item => item.IsVisible);
    }
}

Which one do you prefer ?