gpt4 book ai didi

c# - linq:随机排序

转载 作者:IT王子 更新时间:2023-10-29 03:46:48 27 4
gpt4 key购买 nike

我如何更改下面的代码,每次从数据库中获取 50 个不同的随机数据?

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 语句即可。

如评论所述,使用像 Fisher-Yates Shuffle 这样的算法可能会更好,这是一个实现:https://stackoverflow.com/a/375446/284240

关于c# - linq:随机排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9449452/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com