Dynamic linq query having error if where clause values contain double quote

c# dynamic dynamic-linq linq

Question

I am working on dynamic linq query which generate Where Clause based on some business rules.

It works if input string contains result without having a double quote characters but It throws error once it contains double quote.

string WhereClauseV1  = "Name.Contains(\"stack overflow\")"; --> Works for -> stack overflow

string WhereClasueV2  = "Name.Contains(\"stack\" overflow\")"; --> Throws error for -> stack" overflow

var result = context.Employee.Where(WhereClausev2).ToList();

I am using System.Linq.Dynamic

Also I followed existing stack overflow solution from here. But in my case I have large Where Clause containing multiple AND/OR operator also instance of equal operator I want to use like operator due to that I am using Contains in where clause.

Thanks for any help!!!

1
0
9/24/2019 11:39:35 AM

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\"");
3
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:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

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;
                do
                {
                    NextChar();
                    while (textPos < textLen && ch != quote) NextChar();
                    if (textPos == textLen)
                        throw ParseError(textPos, Res.UnterminatedStringLiteral);
                    NextChar();
                } while (ch == quote);
                t = TokenId.StringLiteral;
                break;

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