LINQ:为了随机 [英] linq: order by random

查看:172
本文介绍了LINQ:为了随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改低于code,每次从数据库中获取50个不同的随机数据?

 在idb.Exam_Question_Int_Tbl回报(从examQ
      其中,examQ.Exam_Tbl_ID == exam_id
      选择examQ).OrderBy(X => x.Exam_Tbl_ID)。取(50);


解决方案

<一个href=\"http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx\">http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx

 在idb.Exam_Question_Int_Tbl回报(从examQ
      其中,examQ.Exam_Tbl_ID == exam_id
      选择examQ).OrderBy(X =&GT; Guid.NewGuid()。)取(50);

如果这是LINQ到SQL,你可以简单地通过NEWID()添加为了您的SELECT语句。

作为评价它可能是最好使用像费雪耶茨洗牌的一种算法,在这里是一个实现: http://stackoverflow.com/a/375446/284240

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);

If this is LINQ-to-SQL you could simply add a ORDER BY NEWID() to your SELECT statement.

As commented it might be better to use an algorithm like Fisher-Yates Shuffle, here is an implementation: http://stackoverflow.com/a/375446/284240

这篇关于LINQ:为了随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆