I currently have code that allows me to check if a number is within a range. I set up and upper and lower bound and then use code like this.
u.RowKey.CompareTo(lowerBound) >= 0 & u.RowKey.CompareTo(upperBound) < 0)
This works for me when the numbers are simple for example something between 00 and 08. However what if I just want to get all the numbers and still use the above. How can I set up my compare to include the numbers. I need to set the upperBound to something but what can I set that开发者_如何学Python is greater than the number "9"?
Row keys look like this:
01-0000
02-0000
89-0000
99-9999
I want to be able to set the upper bound so it is greater than 99-9999
Note that using strings to sort in orders other than alphabetical is fraught with pain. You could try using aa-aaaa
as a hack, but personally I'd be tempted to write a numerical converting sort (a custom comparer), or (probably more efficient) I might add a pre-computed numerical equivalent, i.e. so that "01-0000"
becomes the integer 10000
. Then you just sort on u.KeyNumericValue
instead. For example, you could do this in the RowKey
setter (and make the numeric value read-only, and only changed by changing the key).
Implement a customized comparer, like so:
public class RowKeyComparer : IComparer
{
int Compare(object a, object b)
{
var rowA = (RowKey) a;
var rowB = (RowKey) b;
...
}
}
See also http://support.microsoft.com/kb/320727
Assign the value of 99-9999 to the upperBound
and modify the comparision statement as the following:
u.RowKey.CompareTo(lowerBound) >= 0 & u.RowKey.CompareTo(upperBound) <= 0)
Given that your examples are strings, it seems that you want to check that the strings are valid keys. Assuming minKey = 01-0000
and maxKey = 99-9999
(inclusive bounds if you like) you could check this with a regex rather than using CompareTo at all.
Regex.IsMatch(u.RowKey, @"^\d[1-9]-\d{4}$")
would do it.
精彩评论