所以我需要能够从我的查询返回的一组记录中选择一个随机记录。我在弄清楚如何执行此操作时遇到问题,因为我不确定结果是在哪个容器中提供给我的。我还使用 EntitySpaces 进行数据库持久性,因此大多数数据库交互性都来自那里。
我已经粘贴了应该在下面执行此操作的方法,以及我遇到问题的伪代码。
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)
}
我将不胜感激任何帮助。由于我们使用了 EntitySpaces,我什至不确定此时谷歌会做什么,以及这似乎使几乎所有正常的解决方案都不起作用。
谢谢你。
更新:
所以这就是我到目前为止想出的。现在的问题是,if
当我知道集合中应该有与这些相对应的值时,这些语句被评估为假。
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];
有任何想法吗?