本文介绍了linq:随机排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何更改以下代码,每次从数据库中获取 50 个不同的随机数据?
How can I change below code, to each time get 50 different random data from database?
return (from examQ in idb.Exam_Question_Int_Tbl
where examQ.Exam_Tbl_ID==exam_id
select examQ).OrderBy(x=>x.Exam_Tbl_ID).Take(50);
推荐答案
http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx
return (from examQ in idb.Exam_Question_Int_Tbl
where examQ.Exam_Tbl_ID==exam_id
select examQ).OrderBy(x => Guid.NewGuid()).Take(50);
如果这是 LINQ-to-SQL,您可以简单地将 ORDER BY NEWID()
添加到您的 SELECT 语句中.
If this is LINQ-to-SQL you could simply add a ORDER BY NEWID()
to your SELECT statement.
正如评论的那样,使用像 Fisher-Yates Shuffle,这是一个实现:https://stackoverflow.com/a/375446/284240
As commented it might be better to use an algorithm like Fisher-Yates Shuffle, here is an implementation: https://stackoverflow.com/a/375446/284240
这篇关于linq:随机排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!