Michael H
Michael H

Reputation: 185

Need to pick a random record from query results

So I need to be able to pick a random record from a set of records that my query returns. I'm having an issue figuring out how to do this because I'm not sure what container the results are given to me in. I'm also using EntitySpaces for database persistence, so most of the DB interactivity comes from there.

I've pasted the method that is supposed to do this below, with pseudocode where I'm having an issue.

    protected void btnChoose_Click(object sender, EventArgs e)
{

    DateTime? dateFrom = null;
    DateTime? dateTo = null;

    if (!string.IsNullOrWhiteSpace(dtDateTo.Text))
    {
        dateFrom = dtDateFrom.Text.ToDateTime().Value;
    }
    if (!string.IsNullOrWhiteSpace(dtDateTo.Text))
    {
        dateTo = dtDateTo.Text.ToDateTime().Value.EndOfDay();
    }

    EmployeeRecognitionCollection erc = EmployeeRecognitionCollection.GetAllCards(dateFrom, dateTo, null, null);

    --> I need to figure out what 'erc' actually is so I can figure out how to use Random() appropriately 

    --> I've already verified that erc ends up containing the records that match the criteria in the query (in this case it's just a date range)

}

I'd greatly appreciate any help. I'm not even sure what to Google at this point because of our use of EntitySpaces and how that seems to make virtually every normal solution not work.

Thank you.

UPDATE:

So here's what I've come up with so far. Now the issue is that the if statements are evaluating as false when I know there should be values that correspond to these in the collection.

    EmployeeRecognitionCollection erc = EmployeeRecognitionCollection.GetAllCards(dateFrom, dateTo, null, null);

    int minRecords = 0;
    int maxRecords = 0;

    if (erc[0].ToInteger().HasValue)
    {
        minRecords = erc[0].ToInteger().Value;
    }
    if (erc[erc.Count() - 1].ToInteger().HasValue)
    {
        maxRecords = erc[erc.Count() -1].ToInteger().Value;
    }

    Random r = new Random();
    int winner = r.Next(minRecords, maxRecords);

    EmployeeRecognition er = erc[winner];

Any ideas?

Upvotes: 0

Views: 108

Answers (1)

Ravi M Patel
Ravi M Patel

Reputation: 3035

Update

I couldn't fully understand what you're trying to achieve in your updated code, however, here is my best guess of how it should be written, try it:

Random r = new Random();
int winner = r.Next(0, erc.Count()-1);
EmployeeRecognition er = erc[winner];

From the name EmployeeRecognitionCollection, it appears that it is a collection. If it is possible to debug, what you need to find out using quickwatch is that if it supports indexers. i.e is it ok to do erc[i]. If it is, then using Random on the indexer is fairly simple.

If it doesn't support indexer, you may want to find out if it gives Count property. That way you can get enumerator, advance it i number of times, where i is a random number between 0 and Count-1. To advance you need to call

erc.GetEnumerator().MoveNext()

Upvotes: 0

Related Questions