private static string AddContextToExpression(string originalExpression, string contextName, string generatedDataContextCode) { // Should Match // from _var_ in Customers // let _var_ = Customers.Where(.....) // Should NOT Match // select new { Customers = c } // select new { CustomersSuffix = c } // where "Customers".Length == 9 var expression = originalExpression; // Compile DataContext Assembly assembly = new CodeDomScriptCompiler(new CSharpCodeProvider()).Compile(StandardAssemblies, new[] { generatedDataContextCode }); // Get Table Names var tableNames = GetTableNamesFromDataContextAssembly(assembly); // Replace quoted strings with Tokens Dictionary <string, string> tokens = null; expression = ReplaceQuotedStringsWithTokens(expression, out tokens); // Replace TableNames with ___context.[TableName] foreach (var tableName in tableNames) { // Find tableName - which is preceded by a whitespace, and either followed by a period (.) OR (followed by spaces AND NOT followed by an equal sign) // Avoiding equal sign avoids assignments like LET and SELECT NEW { tableName = .... } string findExpr = string.Format(@"(?<=\s)({0})(?=(\.|\s+[^\s=]))", tableName); // Replace with context prefix string replaceExpr = string.Format(" {0}.$1", contextName); expression = Regex.Replace(expression, findExpr, replaceExpr); } // Replace Tokens with their original string values Regex tokenRegex = new Regex(QuotedStringReplacePrefix + @"[0-9]*"); expression = tokenRegex.Replace(expression, (match) => tokens[match.Value]); return(expression); }
private static string AddContextToExpression(string originalExpression, string contextName, string generatedDataContextCode) { // Should Match // from _var_ in Customers // let _var_ = Customers.Where(.....) // Should NOT Match // select new { Customers = c } // select new { CustomersSuffix = c } // where "Customers".Length == 9 var expression = originalExpression; // Compile DataContext Assembly assembly = new CodeDomScriptCompiler(new CSharpCodeProvider()).Compile(StandardAssemblies, new[] { generatedDataContextCode }); // Get Table Names var tableNames = GetTableNamesFromDataContextAssembly(assembly); // Replace quoted strings with Tokens Dictionary<string, string> tokens = null; expression = ReplaceQuotedStringsWithTokens(expression, out tokens); // Replace TableNames with ___context.[TableName] foreach (var tableName in tableNames) { // Find tableName - which is preceded by a whitespace, and either followed by a period (.) OR (followed by spaces AND NOT followed by an equal sign) // Avoiding equal sign avoids assignments like LET and SELECT NEW { tableName = .... } string findExpr = string.Format(@"(?<=\s)({0})(?=(\.|\s+[^\s=]))", tableName); // Replace with context prefix string replaceExpr = string.Format(" {0}.$1", contextName); expression = Regex.Replace(expression, findExpr, replaceExpr); } // Replace Tokens with their original string values Regex tokenRegex = new Regex(QuotedStringReplacePrefix + @"[0-9]*"); expression = tokenRegex.Replace(expression, (match) => tokens[match.Value]); return expression; }