鉴于以下
class MyClass
{
private Expression<Func<SomeEntity, int>> _orderBy;
public MyClass(Expression<Func<SomeEntity, int>> orderBy)
{
_orderBy = orderBy;
}
public List<SomeEntity> Fetch()
{
return DbContext.Set<SomeEntity>().OrderBy(_orderBy).ToList();
}
}
// A function that creates an orderBy expression
Expression<Func<SomeEntity, int>> SomeFunction()
{
// r is a local variable for the sake of simplicity,
// actually it is a global static variable.
Random r = new Random();
int seed = r.nextInt();
// s.SomeProperty XOR seed, a simple random sorting method
return s => (s.SomeProperty & ~seed) | (~s.SomeProperty & seed);
}
和执行代码
var myClass = new MyClass( SomeFunction() );
List<SomeEntity> someList = myClass.Fetch();
Thread.Sleep(100000);
List<SomeEntity> anotherList = myClass.Fetch();
每次调用 Fetch() 时,都必须返回一个随机排序的列表。问题是,seed = r.nextInt()
每次我调用 Fetch() 时都不会被调用。如何确保每次Fetch()
调用时都会生成一个新种子?