如何以随机顺序返回匹配的实体?
只是为了明确,这是有关Entity Framework和LINQ to Entities的内容。
(代码未经测试)
IEnumerable<MyEntity> results = from en in context.MyEntity
where en.type == myTypeVar
orderby ?????
select en;
感谢
编辑:
我尝试将此添加到上下文中:
public Guid Random()
{
return new Guid();
}
使用以下查询语句:
IEnumerable<MyEntity> results = from en in context.MyEntity
where en.type == myTypeVar
orderby context.Random()
select en;
但我收到了这个错误:
System.NotSupportedException: LINQ to Entities does not recognize the method 'System.Guid Random()' method, and this method cannot be translated into a store expression..
编辑(当前代码):
IEnumerable<MyEntity> results = (from en in context.MyEntity
where en.type == myTypeVar
orderby context.Random()
select en).AsEnumerable();