【发布时间】:2015-04-12 12:53:11
【问题描述】:
所以我需要能够从我的查询返回的一组记录中选择一条随机记录。我在弄清楚如何执行此操作时遇到问题,因为我不确定结果是在哪个容器中提供给我的。我还使用 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 语句在我知道集合中应该有对应于这些值的值时被评估为 false。
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];
有什么想法吗?
【问题讨论】:
-
您使用什么语言?请为该语言添加标签。
-
哎呀,对不起。添加了 C# 标签。
标签: c# sql random entityspaces