Here's an example using the GreaterThanOrEqual
and LessThan
operators, as an extension on the target column name.
The filter combines two parts:
- anything greater than or equal to your startsWith prefix,
- increment the last character of your prefix and find anything less than this.
For example, startsWith
prefix "CAR" will prepare something like s >= "CAR" && s < "CAS"
.
public static string GetStartsWithFilter(this string columnName, string startsWith)
{
var length = startsWith.Length - 1;
var nextChar = startsWith[length] + 1;
var startWithEnd = startsWith.Substring(0, length) + (char)nextChar;
var filter = TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition(columnName, QueryComparisons.GreaterThanOrEqual, startsWith),
TableOperators.And,
TableQuery.GenerateFilterCondition(columnName, QueryComparisons.LessThan, startWithEnd));
return filter;
}
Usage:
var query = new TableQuery<MyTableEntity>().Where(myColumnName.GetStartsWithFilter(prefix));
Based on Alexandre B's blog post