Skip to content

Instantly share code, notes, and snippets.

@takeshik
Created March 19, 2010 05:40
Show Gist options
  • Save takeshik/337268 to your computer and use it in GitHub Desktop.
Save takeshik/337268 to your computer and use it in GitHub Desktop.
public IEnumerable<Activity> ExecuteQuery(String query)
{
return query.Split(Environment.NewLine.ToCharArray())
.Select(s => "(?<method>where|select|orderby|take|skip): *(?<body>.+?)(?: params: *(?<params>.+))?$".RegexMatch(s))
.Select(m => new
{
Method = m.Groups["method"].Value.ToLower(),
Body = m.Groups["body"].Value,
Parameters = m.Groups["params"].Value.Split(',').Select(s => s.Trim(' ')).ToArray()
})
.ZipWith(Make.Repeat(this.DataCache.AsQueryable()), (_, q) =>
{
switch (_.Method)
{
case "where":
return q.Where(_.Body, _.Parameters);
case "select":
return q.Select(_.Body, _.Parameters);
case "orderby":
return q.OrderBy(_.Body, _.Parameters);
case "take":
return q.Take(_.Body, _.Parameters);
case "skip":
return q.Skip(_.Body, _.Parameters);
default:
throw new ArgumentException("Invalid method: " + _.Method);
}
})
.First()
.Cast<Activity>()
.AsEnumerable();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment