Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Trick to transform the SQL that we get from Linqpad so it runs in Tableau and SQL management studio.
void Main()
{
TextWriter tw = new StringWriter();
Log = tw;
Users.Where(e => ...).ToArray();
tw.ToString().TransformSql().Dump();
}
public static class MyExtensions
{
public static string TransformSql(this string input)
{
string[] lines = input.Split('\n').Select(e => e.Trim()).Where(e => !string.IsNullOrEmpty(e)).ToArray();
string sql = string.Join(Environment.NewLine, lines.Where(e => !e.StartsWith("--")).ToArray());
string[] comments = lines.Where(e => e.StartsWith("--")).ToArray();
foreach (string comment in comments)
{
var regex = new Regex(@"^-- (@p\d+): Input (?:\w+)[^/[]*\[(.*)]");
var match = regex.Match(comment);
if (match.Success)
{
string variable = match.Groups[1].Value;
string value = match.Groups[2].Value;
sql = sql.Replace(variable, double.TryParse(value, out _) ? value : $"'{value}'");
}
}
return sql;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.