linq实现随机查询
2015-09-10 21:32
351 查看
<span style="font-size:24px;"> 实例: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace suichaxun { class Program { static void Main(string[] args) { BidSystemEntities dbContext = new BidSystemEntities(); var result = (from c in dbContext.T_SpecialistInfo where c.SpecialistType=="电子类" orderby(Guid.NewGuid())select c).Take(2).ToList(); foreach (var item in result) { Console.WriteLine(item.SpecialistName); } } } }</span>
SQL(通用于 SQL Server)
SELECT FROM Table ORDER BY NewID()
LINQ
t in Table orderby Guid.NewGuid()t
Lambda
Table.OrderBy(x=>Guid.NewGuid())
下面是随机取去前n条数据
SQL(通用于 SQL Server)
SELECT TOP n FROM Table ORDER BY NewID()
LINQ
(fom t in Table orderby Guid.NewGuid()t).Take(n)
Lambda
Table.OrderBy(x=>Guid.NewGuid()).Take(n)
相关文章推荐
- Linux中如何杀掉僵尸进程
- 使用EF框架的增删改查和分页的公共类
- 对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性。
- entity framework的配置步骤
- MVC中“The model backing the '***Context' context has changed since the database was created. ”的问题处理
- vs2012通过EF连接mysql数据库全程
- EF code-first 数据迁移
- 更新条目时出错。有关详细信息,请参见内部异常 错误原因
- EF6.0 生成的代码中没有注释的解决方法
- 在使用EF时,想要比较字符串类型的日期时
- EF code first attribute
- Windows服务之EF加载数据
- 从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值。
- 如何知道错误:Validation failed for one or more entities. See 'EntityValidationErrors' 的原因
- EF-增删改查简单示例
- 用EF实现多条件查询
- 一种基于EF+MVC的小型系统快速开发架构
- ASP.NET MVC+Entity Framework 4.1访问数据库 (EF)
- 利用Migration同步模型与数据库的字段
- EF添加和附加实体对象的状态