Question

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.

Thanks.

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

Était-ce utile?

La solution

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

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top