from f in db.Table1
orderby Guid.NewGuid()
select f
这似乎不起作用。我如何随机化结果?
答案 0 :(得分:1)
怎么样
SELECT TOP 1列FROM表ORDER BY NEWID并跳过linq:)
或试试这个:
var t = (from row in db.Table1 order by table1.random()
select row).FirstOrDefault();
答案 1 :(得分:0)
也许这样的事情有效(未经测试):
(from f in db.Table1 select new { f, r = Guid.NewGuid()}).OrderBy(x => x.r)
答案 2 :(得分:0)
随机化整个列表
db.Table1.OrderBy(x => Guid.NewGuid())
获取单个随机
db.Table1.OrderBy(x => Guid.NewGuid()).FirstOrDefault();
答案 3 :(得分:0)
我喜欢为此编写扩展方法。
IEnumerable<T> Randomize(this IEnumerable<T> list)
{
T[] result = list.ToArray();
Random random = new Random();
for(int i = result.Length; i > 0; i--)
{
result[i] = random.Next(i);
}
return (result);
}