Dynamic LINQ Where-clause Syntax
Dynamic LINQ allows the filter conditions in "Where" clauses to be specified as string expressions similar to a WHERE clause in SQL, however without the risk of malicious SQL injection. The syntax for dynamic Where string expressions is summarized below.
Literals
Symbol |
Meaning | Examples |
---|---|---|
String literal | String Literals are enclosed in double quotes. Note In many client environments and languages (e.g. the "C" family of languages), double quotes are used to define strings, while single quotes are used for single-character literals. In such cases, the double quotes that indicate a literal string within a filter string must be escaped by preceding the double quote character with a backslash ( \ ). |
"A string" "A so-called \"string\"." |
Date literal | Date literals are written as formatted date strings enclosed in # symbols | #03/21/2024# |
true |
Boolean true value* | true |
false |
Boolean false value* | false |
null |
Null value | null |
* Note that if a Boolean-valued variable (suppose this is named IsThisTrue
) is included in the filter string, then the expressions
"IsThisTrue"
and
"IsThisTrue == true"
do not behave the same way if IsThisTrue
is null
. The first case evaluates to null
if IsThisTrue
is null
, while the second case evaluates to false
.
Comparison Operators
Symbol | Meaning | Examples |
---|---|---|
== | Equal to | "Status == \"Open\"" "Count == 0" "theDate == #01/04/2025#" |
<> | Not Equal to | "Status <> \"Open\"" "Count <> 0" "theDate <> #01/04/2025#" |
> | Greater Than | Name > \"Michael\" "Count > 0" "theDate > #01/04/2025#" |
< | Less Than | "Name < \"Michael\"" "Count < 10" "theDate < #01/04/2025#" |
>= | Greater Than Or Equal To | "Name >= \"Michael\"" "Count >= 1" "theDate >= #01/04/2025#" |
<= | Less Than Or Equal To | "Name <= \"Michael\" "Count <= 10" "theDate <= #01/04/2025#" |
String Operators
Note: Dynamic LINQ does not natively support the LIKE
operator, but similar results can be used with the StartsWith()
, Contains()
and EndsWith()
functions.
Function | Meaning | Examples |
---|---|---|
StartsWith(testStr) |
True if the string to be tested starts with testStr |
"Name.StartsWith(\"John'\")" Matches "John Brown" |
Contains(testStr) |
True if the string to be tested contains testStr |
"Name.Contains(\"ich\")" Matches "Michael", "Michelle" and "Richard" |
EndsWith(testStr) |
True if the string to be tested ends with testStr |
"Name.EndsWith(\"Smith\")" Matches "Mary Smith" |
Length |
Returns the length of a string | "Message.Length > 300" |
ToLower() |
Returns a string with all lower-case characters | "MixedCaseString.ToLower() == \"all lower case\"" |
ToUpper() |
Returns a string with all upper-case characters | "MixedCaseString.ToUpper() == \"ALL UPPER CASE\"" |
Collection Operators
Function |
Meaning | Examples |
---|---|---|
in (collection) |
True if the value of the preceding symbol is a member of the collection | "DrinkType in (\"Water\",\"Beer\",\"Wine\",\"Juice\")" |
Count |
Returns the number of elements in a collection | "Drinks.Count > 5" |
Nullity Tests
Keywords | Meaning | Examples |
---|---|---|
is null |
True if the preceding symbol has a null value | "Name is null" |
is not null |
True if the preceding symbol does not have a null value | "Name is not null" |
Logical Operators
Keywords | Meaning | Examples |
---|---|---|
AND |
True if the preceding and following conditions are both true | " X > 0 AND X < 10" |
OR |
True if one or both of the preceding and following condition are true | " X < 0 OR X > 10" |
NOT |
True if the condition following the keyword is false | "NOT SomeCondition" |