-1

I have a table named Article which contains 6 different values of partition key and under each partitions key there are 100's of entities, but under each partition key I want to retrieve only n entities back from table. So, I want to query such that it only returns 'n' random entities using partition key only?

rahul desai
  • 81
  • 1
  • 6
  • It's easy to return 'n' entities -- you can just use the top query along with a filter on the partition key. However, this will return the first 'n' entities alphabetically by row key in that partition. The random requirement is sort of odd and definitely not supported by default. Can you clarify more the intent of this requirement? – Emily Gerner Feb 25 '16 at 17:36

1 Answers1

0

I think there is no direct solution. But you can simulate retrieving of random values by specifying random where conditions on CustomKey. CustomKey is technological field, it needed only for solving your problem and has int type. When you adding new item to Azure Table you can assign to this field random value between 0 and 100, for example. Then you can write something like this (based on this article):

var rand = new Random();
var query = from entity in context.CreateQuery<Customer>("CustomersOver30")
            where entity.PartitionKey == "MyPartitionKey" && 
            entity.CustomKey > rand.Next(0, 50) && entity.CustomKey < rand.Next(50, 100)
            select entity;

You will retrive some number of entities, if it will be more or equal your n, just take n items from it. If count of items will be less then n, just repeat this operation untill you will have enought number of items.

Yes, it is hack, but azure tables have very restricted opportunities - only where and top operations are supported.

Slava Utesinov
  • 13,410
  • 2
  • 19
  • 26
  • My row key is not a integer type. It is actually a string vale. Let me give you one row key as example: "effects-filter-choice|25". – rahul desai Feb 25 '16 at 09:57
  • I corrected my answer. Instead of `RowKey` use `CustomKey` as described above. – Slava Utesinov Feb 25 '16 at 10:59
  • @Salva : What is custom key? And I want to write a query string for this. string combineQuery = TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, journal.RowKey), TableOperators.And, TableQuery.GenerateFilterCondition("RowKey", ......, ........)); What should come in blank spots?To make it correct? – rahul desai Feb 25 '16 at 11:21
  • CustomKey is field, that you will add to your model manually and save with other data it helps you to get random n items by means of query above. You will not use RowKey at your filter builder(as shown in your comment), because it is not easy to create condition with RowKey that will return n random items, because it has string type. Instead of RowKey you will create CustomKey with int type and create filter with it, because it is very easy (with help of TableQuery.GreaterThan and LessThan random.Next() look at my query). – Slava Utesinov Feb 25 '16 at 11:38
  • @Salva : Can you please tell me how can I write Query string for the same? – rahul desai Feb 25 '16 at 12:16
  • TableQuery.GenerateFilterCondition("CustomKey", QueryComparisons.GreaterThan, rand.Next(0, 50)), TableOperators.And, TableQuery.GenerateFilterCondition("CustomKey", QueryComparisons.LessThan , rand.Next(50, 100)), TableOperators.And, TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "MyPartitionKey") – Slava Utesinov Feb 25 '16 at 12:19