I use System.Linq.Dynamic to query entities with dynamic 'where' expressions. I'm querying object that has property "newValue" of string type. Exemplary value would be : "{\"ProcessId\":764, \"ProcessLength\":1000}". I can't use == because I want to find all hits where the property contains "ProcessId:764", regardless on the rest of the string. The thing is, that stored string contains escape sign "\" and double quotes and I can't figure out what it should like exactly..
dbContext.Processes.Where("@newValue.Contains(\"ProcessId\":764\")")
brings error, however dbContext.Processes.Where("@newValue.Contains(\":764\")")
works correctly. I guess it must be something with backslashes or double quotes in my query but can't figure it out on my own..
There are two things to note here:
If you know at compile time the column that should be queried (i.e.,
newValue
), just use standard Linq:var list = items.Where(i => i.NewValue.Contains("904")).ToList()
.If you do want to use dyanmic Linq, What you'd usually want is to apply
Where
on some column, e.g.Where("SomeColumn.Contains("something")")
, orWhere("SomeColumn.Contains(@0)", new string[] {"something"})
.So, in your case, this should work:
items.Where("newValue.Contains(\"904\")")
.Doing
Where("@newValue.Contains("something")")
doesn't really make sense, since@newValue
would be parsed as a string literal. See also this comment on a similiar question.Here' a quick example: