Created
April 30, 2010 01:58
-
-
Save johnnyhalife/384604 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class AzureTableStringHelpers | |
{ | |
public static string AsEncodedKey(this string key) | |
{ | |
if (key == null) | |
{ | |
return null; | |
} | |
return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(key)); | |
} | |
public static string DecodeFromKey(this string encodedKey) | |
{ | |
if (encodedKey == null) | |
{ | |
return null; | |
} | |
return System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(encodedKey)); | |
} | |
} | |
/* The Tests */ | |
[TestMethod] | |
public void EncodeAndDecodeKey() | |
{ | |
var encodedPartitionKey = "ADATUM\\Mary".AsEncodedKey(); | |
var actualPartitionKey = encodedPartitionKey.DecodeFromKey(); | |
Assert.AreEqual("ADATUM\\Mary", actualPartitionKey); | |
} | |
/* Real World Usage */ | |
var context = new ExpenseDataContext(this.account); | |
var query = | |
(from expense in context.Expenses | |
where expense.PartitionKey.CompareTo("ADATUM\\Mary".AsEncodedKey()) == 0 | |
select expense).AsTableServiceQuery(); | |
return query.Execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment