Skip to content

Instantly share code, notes, and snippets.

@mharju
Created October 20, 2011 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mharju/1300745 to your computer and use it in GitHub Desktop.
Save mharju/1300745 to your computer and use it in GitHub Desktop.
Playing around with functional features of C# to get concise code. This style does not feel so idiomatic, though.
OpenReadCompletedEventHandler GetGenericOpenHandler<T>(ObservableCollection<T> destination, Func<XElement, IEnumerable<T>> elementQuery)
{
return (object sender, OpenReadCompletedEventArgs e) =>
{
XElement xmlResult = this.GetXmlResult(e);
foreach (T result in elementQuery(xmlResult))
{
destination.Add(result);
}
};
}
private XElement GetXmlResult(OpenReadCompletedEventArgs e)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Ignore;
XmlReader reader = XmlReader.Create(e.Result, settings);
return XElement.Load(reader);
}
// Ja sitten voi tehdä jokaiselle feedille mitä on:
this.LoadFeedByName("results",
this.GetGenericOpenHandler(Results,
(XElement xmlResult) =>
{
return from result in xmlResult.Descendants("game")
select new ResultModel
{
Away = result.Element("team_home").Value.Trim(),
Home = result.Element("team_away").Value.Trim(),
Scores = result.Element("scores").Value.Trim(),
GameDate = DateTime.Parse(
new Func<string>(() =>
{
string[] date_parts = result.Parent.Attribute("date").Value.Trim().Split('.');
return date_parts[2] + "/" + date_parts[1] + "/" + date_parts[0];
})())
};
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment