DynamicLINQ - Escaping double quotes inside strings

c# dynamic-linq


I'm trying to do a dynamic filtering system using the DynamicLINQ library. I have everything working smoothly when you do something like: Find people with First Name is Bob:

Context.Users.Where("FirstName == \"Bob\"");

But I run into problems when I want to do: Find people with First Name is "Bob" (where Bob is stored in double quotes in the data source).

I tried a few different things, including escaping an escaped double quote and a few other variants:

Context.Users.Where("FirstName == \"\\\"Bob\\\"\"");
// or as a literal for readability
Context.Users.Where(@"FirstName == ""\""Bob\""""");
// From comments below
Context.Users.Where("FirstName == \"\"Bob\"\"");

None of these work. Any help would be greatly appreciated.


EDIT - I'm just dealing with the resulting string right now. The actual string is generated from a model.

4/21/2014 5:57:16 PM

Accepted Answer

If you want use in clause some specific string with special symbols then better way, as i think, use paramtrized form like this

Context.Users.Where("FirstName == @0", "\"Bob\"");
4/22/2014 5:13:23 AM

Popular Answer

My thought is you can not use .Where() to do dynamic linq evaluations as you have written. The reason is because Where() does not understand what FirstName is, and was never intended to do dynamic Linq expressions. You would use where like the following .Where( x => x.FirstName == "\"Bob\""); and that will work for sure.

A good head start is to use an existing Library found on ScottGu's Blog as follows:


He has a download with code that will do everything you are describing. It will take a little time to digest the library but I have used it in a project and it works great. You need to know a little bit about Lambdas and you will go far.

Hope this helps :) Good Question, I have been there and done that. It was tricky finding this solution.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ EDIT


Look at Dynamic.cs:

Line 2123 the following code exists in the method ParseToken().

            case '"':
            case '\'':
                char quote = ch;
                    while (textPos < textLen && ch != quote) NextChar();
                    if (textPos == textLen)
                        throw ParseError(textPos, Res.UnterminatedStringLiteral);
                } while (ch == quote);
                t = TokenId.StringLiteral;

What this parser appears to be doing is: when it reads the second " in [""Bob""] it returns a null string Literal, thinking it has found the end of the string literal, then it would parse an identifier [Bob] and then another null string literal. Somehow you will have to modify the parser to look for "" as a token.

Maybe in ParseComparison() on line 766 you can devise a way to look for null String Literal followed by an identifier followed by another null String Literal. ???

Easy solution is to Replace " with null since rewriting the parser looks like some major effort.

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow