有没有办法选择随机行? [英] Is there a way to select random rows?

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

问题描述

此时我并不关心它是L2E还是ObjectQuery。我只是为这个TSQL拍摄......

At this point I don't really care if it's L2E or ObjectQuery. I'm just shooting for this TSQL...

SELECT TOP 10 * from myTable ORDER BY NewID()

SELECT TOP 10 * FROM myTable ORDER BY NewID()

推荐答案

是的......但SQL(幕后)并不漂亮。 假设您的模型被称为MyEntities并且您正在追踪公司集合

Yes...But the SQL (behind the scenes) is not as pretty.  This gets the job done assuming your model is called MyEntities and you are going after a company collection

using (MyEntities ctx = new MyEntities())
{
    IQueryable<Company> result = ctx.Companies.OrderBy(c => Guid.NewGuid()).Take(10);
    foreach (Company company in result)
    {
       Console.WriteLine(company.CompanyName1);
    }
    //This is the t-sql
    Console.WriteLine(((System.Data.Objects.ObjectQuery)result).ToTraceString());
}


这篇关于有没有办法选择随机行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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