Geeks With Blogs
IUnknown Windows Azure mumblings of IUnknown

Last time we saw some interesting initialization constructs possible with LINQ. Hope some of you have tried it out. If not, it’s never too late. LinqPad is a good friend in need.

This time around we’d take a look at two common looping scenarios.

1. Looking up an object by name:

Object lookup is commonplace while dealing with collections. Let’s look for an object with a given name. Previously we’d run a for each and come up with something like.

public MyObject FindByName(string value)
 
{
 
foreach (MyObject obj in objects)
 
if (obj.Name == value)
 
return obj;
 
return null;
 
}
 

With LINQ this could be simplified to.

public MyObject FindByName(string value)
return (objects.Where(obj => obj.Name == value).FirstOrDefault());
 
  // Chose First or FirstOrDefault depending on probability of getting a no match. 
 
// Wise choice here would help reducing exception handling semantics
 
}
 

2. Changing a property of some/all elements in a collection:
We often need to change a property for some/all elements in a collection. E.g. Changing the background of all TextBoxes on a form. This could be worded as “Changing the BackColor property of all Controls of type TextBox on a Form)

In the days bygone (Not exactly bygone yet :)) we’d use something like:

public void ColorTextBoxes()
{
    // Loop through all controls on the form.
    foreach (Control ctrl in this.Form.Controls)
    {
        // Ensure it is a TextBox.
        if (ctrl is TextBox)
        {
            // Cast to a TextBox and make the background Windows Azure.
            ((TextBox)ctrl).BackColor = System.Drawing.Color.Azure;
         }
    }
}

With LINQ we could conjure up something like

public void ColorTextBoxes() 
{ 
    (from control in this.Form.Controls.Cast<Control>() where control is TextBox select control).ToList().ForEach(t => ((TextBox)(t)).BackColor = System.Drawing.Color.Azure);
}

The query looks cryptic to start, however if you look closely its fairly straightforward. We’d first cast the ControlCollection to an IEnumerable of Controls with the Cast<>() extension. Then we filter only the TextBoxes with where.  Then we use ToList() to expedite query processing otherwise lazy evaluation would be used. Then the List.ForEach() would iteratively process the parameter statement which simply changes the BackColor property of TextBox to Windows Azure.

Posted on Monday, March 1, 2010 10:29 AM | Back to top


Comments on this post: Thou Shalt not Iterate! : Part 2

# re: Thou Shalt not Iterate! : Part 2
Requesting Gravatar...
You can eliminate the where statement if you replace the Cast() with OfType<TextBox>()
Left by mrjimmy on Mar 09, 2010 3:26 AM

# re: Thou Shalt not Iterate! : Part 2
Requesting Gravatar...
Thanks mrjimmuy, that'd be an interesting weay to do it as well. :)
Left by IUnknown on Mar 09, 2010 2:25 PM

Your comment:
 (will show your gravatar)


Copyright © Sarang | Powered by: GeeksWithBlogs.net