I am using System.Linq.Dynamic;
to generate dynamic linq query which featch records from db context.
I generate where clause in string format for dynamic linq, It works perfectly if input does not have any double quote. However, it throws error once input string contains double quote.
I followed Stack Overflow existing solution dynamiclinq-escaping-double-quotes-inside-strings but it does not helped because I wanted to use LIKE operator.
I'm using Contains condition in my where clause because I need LIKE operator for query. Please follow below sample code...
//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 execute above query it throws error as ')' or operator expected
. But as per above where clause it looks all bracket are fine not sure is it any issue with dynamic query dll.
Thank you for any advice.
You need to escape the escape symbol \
.
//name should contains "6"" WHITE COLOR
var whereClause = "(!flag.Contains(\"D\")) AND (Name.Contains(\"\\\"6\\\"\\\" WHITE COLOR\"))";
var result = context.Product.Where(whereClause).ToList();
Or use 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();