Reputation: 24572
I have this code:
var query = _cityRepository.GetAll(
u => u.PartitionKey == pk &
u.RowKey.CompareTo(lowerBound) >= 0 &
u.RowKey.CompareTo(upperBound) < 0)
.OrderBy(item => item.RowKey.Substring(0, 3))
.ThenBy(item => item.ShortTitle)
.Select((t, index) => new City.Grid()
{
PartitionKey = t.PartitionKey,
RowKey = t.RowKey,
Row = index + 1,
ShortTitle = t.ShortTitle,
Created = t.Created,
CreatedBy = t.CreatedBy,
Modified = t.Modified,
ModifiedBy = t.ModifiedBy
})
.ToList();
When I look at the data coming out I find this for the first two rows:
RowKey = 0101004O , ShortTitle = "Access 1"
RowKey = 0103004M , ShortTitle = "Access 2"
RowKey = 0101004K , ShortTitle = "xxx"
When testing I simplified this to:
var query1 = _cityRepository.GetAll()
.OrderBy(item => item.RowKey.Substring(0, 3))
.ThenBy(item => item.ShortTitle);
and the order is the same. Still the RowKey does not seem to be in the right order.
It is supposed to sort on the first four characters of the rowkey and then on the ShortTitle.
Can anyone see why this is not working. I have looked hard but I can't see why the OrderBy and ThenBy doesn't seem to work correctly.
Upvotes: 1
Views: 73
Reputation: 4730
Change your substring to Substring(0, 4) if you want to get 4 chars. 2nd parameter specifies length not the index
Upvotes: 1
Reputation:
Substring
's second parameter is length
, not index, which I believe is what your code is attempting to do. Your code in its current state is sorting based on the first three characters. Change:
.OrderBy(item => item.RowKey.Substring(0, 3))
to
.OrderBy(item => item.RowKey.Substring(0, 4))
Upvotes: 2
Reputation: 86084
You're sorting by the first 3 characters of rowkey
, not 4. To use 4 characters, do this:
.OrderBy(item => item.RowKey.Substring(0, 4))
Upvotes: 5