Reputation:
I'm trying to upgrade the MS PnP CQRS project to the latest Azure SDK and I have the following 2 queries:
var query = new TableQuery<EventTableServiceEntity>().Where(
TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual, UnpublishedRowKeyPrefix),
TableOperators.And,
TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThanOrEqual, UnpublishedRowKeyPrefixUpperLimit)))
.Select(x => new { x.PartitionKey })
.AsTableQuery();
var query2 = eventTableServiceEntities
.Where(
x =>
String.Compare(x.RowKey, UnpublishedRowKeyPrefix, StringComparison.Ordinal) >= 0 &&
String.Compare(x.RowKey, UnpublishedRowKeyPrefixUpperLimit, StringComparison.Ordinal) <= 0)
.Select(x => new { x.PartitionKey }).AsTableQuery();
The 1st one doesn't error (the query I think is wrong anyway) The 2nd one is the original one and that now errors with object reference not set to an instance of an object
.
1: what is the issue with the 2nd query? Is this style not supported anymore? I haven't even gotten to the point where it's being executed!
2: What is the 2nd query doing and how would I represent that in the style of query 1 if the linq style is resigned.
Here is the original code:
The sort of stuff I am confused about here is
x.RowKey.CompareTo(UnpublishedRowKeyPrefix) >= 0
Where UnpublishedRowKeyPrefix
is:
private const string UnpublishedRowKeyPrefix = "Unpublished_";
How can you compare that meaningfuly? What am I missing?!
eventTableServiceEntities is from here - I created a variable to aid debugging:
var eventTableServiceEntities= new TableQuery<EventTableServiceEntity>();
var query2 = eventTableServiceEntities
.Where(
x =>
String.Compare(x.RowKey, UnpublishedRowKeyPrefix, StringComparison.Ordinal) >= 0 &&
String.Compare(x.RowKey, UnpublishedRowKeyPrefixUpperLimit, StringComparison.Ordinal) <= 0)
.Select(x => new { x.PartitionKey })
.AsTableQuery();
Upvotes: 1
Views: 562
Reputation: 240
The string comparison you provided works in the following manner: when you filter on values >= "a" and < "b", you will get back all strings that start with "a". So for your example, it looks like the filter is for all strings that start with "Unpublished_" but are lower than the UnpublishedRowKeyPrefixUpperLimit you have set. As for help with crafting Linq queries, this link should help you understand the differences between Fluent mode and the newer IQueriable mode for writing queries and how to convert from one to the other. (Relevant content is halfway down the page.)
Upvotes: 2