Thursday, September 10, 2009

One liners in .Net

Just wanted to show a few one liners from a project I'm working on. I get a lot of data from a third party as XML. In my business logic I want to work with objects so I have made a class that wraps on entity in the XML.

var orgs = doc.SelectNodes("//Organisation").Cast<xmlnode>().Do<xmlnode,wrappedorganisation>(x => new WrappedOrganisation(x));

So in this line all the organisations in the xml are located and a list of WrappedOrganisation is created from it. With the next one we had to hide a lot of textboxes on a page if a related hidden field had the value "0".

Page.findControl<placeholder>().Do(x => x.Visible = x.findControl<hiddenfield>().Count(h => h.Value != "0") != 0);

The next one retrieves a list of custom objects from some textboxes on a UI.

IList<customvalues> list = UserControl.findControl<textbox>().Do(x => timeSpecs.Add(new CustomValues() { Amount = int.Parse(x.Text), Month = StartDate.Value.AddMonths(i++) }));

Finally a line that identifies TextBoxes on a UI with a value that is not an integer and marking them.

UserControl.findControl<textbox>().Where(x => !int.TryParse(x.Text, out i)).Do(x => x.CssClass = "borderRed").Do(x => messages.Add("Not a valid amount"));