EF code第一:如何获得随机行 [英] EF Code First: How to get random rows
问题描述
我怎样才能建立一个查询在那里我会找回随机行?
How can I build a query where I would retrieve random rows?
如果我是把它写在SQL然后我会在NEWID把一个ORDER BY()和砍掉从顶部n行。反正先做这EF code?
If I were to write it in SQL then I would put an order by on newid() and chop off n number of rows from the top. Anyway to do this in EF code first?
我曾尝试创建一个使用NEWID(查询),并使用DbSet.SqlQuery执行它()。而它的工作原理,它不是最干净的解决方案。
I have tried creating a query that uses newid() and executing it using DbSet.SqlQuery(). while it works, its not the cleanest of solutions.
此外,试图检索所有的行和由一个新的GUID排序它们。虽然行的数量是相当小的,它仍然没有很好地解决。
Also, tried retrieve all the rows and sorting them by a new guid. Although the number of rows are fairly small, its still not a good solution.
任何想法?
推荐答案
只要致电:
something.OrderBy(r => Guid.NewGuid()).Take(5)
这篇关于EF code第一:如何获得随机行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!