相关疑难解决方法(0)

Linq to Entities,随机顺序

如何以随机顺序返回匹配的实体?
要明确这是Entity Framework的东西和LINQ to Entities.

(航空代码)

IEnumerable<MyEntity> results = from en in context.MyEntity
                                where en.type == myTypeVar
                                orderby ?????
                                select en;
Run Code Online (Sandbox Code Playgroud)

谢谢

编辑:
我尝试将其添加到上下文中:

public Guid Random()
{
    return new Guid();
}
Run Code Online (Sandbox Code Playgroud)

并使用此查询:

IEnumerable<MyEntity> results = from en in context.MyEntity
                                where en.type == myTypeVar
                                orderby context.Random()
                                select en;
Run Code Online (Sandbox Code Playgroud)

但我得到了这个错误:

System.NotSupportedException: LINQ to Entities does not recognize the method 'System.Guid Random()' method, and this method cannot be translated into a store expression..
Run Code Online (Sandbox Code Playgroud)

编辑(当前代码):

IEnumerable<MyEntity> results = (from en in context.MyEntity
                                 where …
Run Code Online (Sandbox Code Playgroud)

c# linq-to-entities entity-framework

36
推荐指数
3
解决办法
4万
查看次数

标签 统计

c# ×1

entity-framework ×1

linq-to-entities ×1