Linq dynamic having issue with double quote

dynamic dynamic-linq linq sql-server

Question

I amusing System.Linq.Dynamic; to create a dynamic Linq query that fetches data from the database context.

I create where clauses for dynamic linq in string format, and if the input has no double quotes, they operate flawlessly. However, if the supplied string includes a double quotation, an error is raised.

Because I intended to utilize the LIKE operator, I tried the Stack Overflow existing solution dynamiclinq-escaping-double-quotes-inside-strings, but it was ineffective.

In my where clause, I have the condition Contains since my query needs the LIKE operator. Please use the example code below...

//name should contains  "6"" WHITE COLOR

var whereClause = "(!flag.Contains(\"D\")) AND (Name.Contains(\"\"6\"\" WHITE COLOR\"\"))";

var result = context.Product.Where(whereClause).ToList();

Once I run the aforementioned query, an error appears as')' or operator expected . However, based on the where clause above, it seems like every bracket is in place, thus it's unclear if the dynamic query dll is to blame.

Thank you for any suggestions.

1
0
9/24/2019 2:13:28 PM

Accepted Answer

You must use the escape key.\ .

//name should contains  "6"" WHITE COLOR

var whereClause = "(!flag.Contains(\"D\")) AND (Name.Contains(\"\\\"6\\\"\\\" WHITE COLOR\"))";

var result = context.Product.Where(whereClause).ToList();

may make advantage of parameterization.

//name should contains  "6"" WHITE COLOR

var flag = "\"D\"";
var name = "\"6\"\" WHITE COLOR";
var whereClause = "(!flag.Contains(@0)) AND (Name.Contains(@1))";

var result = context.Product.Where(whereClause, @0, @1).ToList();
0
9/24/2019 2:25:43 PM


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