1

I'm new to Dynamics 365 development. I have an activity where I need to select a certain number of random records in an entity, it would be like a prize draw from a list of records. I will be working with many records. How to best do this in C#?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

There isn't a built-in functionality to get a random record, one approach (probably is not the fastest one) is to retrieve all the records first, if you are using C# you can do a RetrieveMultiple (keep in mind that you need pagination if the records are more than 5000) with just the Id (so ColumnSet can be false).

After you retrieved the Ids you can put them in a List and access (randomly) the elements you need (check this link as an example How to access random item in list?).

Guido Preite
  • 14,905
  • 4
  • 36
  • 65
  • Thank you very much Guido, the downside is actually the runtime. I thought of putting a field as an increasing number on the form. so I can take the amount of records created and randomize this field, then getting the record referring to this number. Sorry for my English, I'm from Brazil and I'm still learning the new language. – Marcelo Mariniello Nov 19 '21 at 13:57
  • also your approach is valid, you can create an autonumber attribute and make it unique (as alternate key). With autonumber can happen that a number doesn't exist (for example if a record get deleted) but with a retry condition on your logic it can be implemented. thanks for sharing your approach. – Guido Preite Nov 19 '21 at 14:33
  • just noticed @Aron gave a similar answer in his comment, my apologies – Guido Preite Nov 19 '21 at 14:34
0

Building on Guido's answer, another idea would be to get the minimum CreatedOn and maximum CreatedOn for the records in question. Then generate a random date and time within that range (inclusive of the min and max). To get the random record, query for the top 1 where CreatedOn is greater than the random date, order by CreatedOn.

Or, if you count the records first, you can generate a random number in that range and do a LINQ query with Skip(random #).Take(1).

Aron
  • 3,877
  • 3
  • 14
  • 21
  • Thank you very much Aron. I hadn't thought of createdon. I thought of putting a field as an increasing number on the form. so I can take the amount of records created and randomize this field, then getting the record referring to this number. Sorry for my English, I'm from Brazil and I'm still learning the new language. – Marcelo Mariniello Nov 19 '21 at 14:02
  • Bom dia! You're welcome. I like your idea to number the records. With the out-of-box [autonumbering](https://learn.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/create-auto-number-attributes?view=op-9-1) you could easily add a counter field to the entity and then randomly select a record (with some caveats - like autonumbers could potentially have gaps, and you need to back-populate values on existing records). – Aron Nov 19 '21 at 14:26