Given the following
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);
}
And the executing code
var myClass = new MyClass( SomeFunction() );
List<SomeEntity> someList = myClass.Fetch();
Thread.Sleep(100000);
List<SomeEntity> anotherList = myClass.Fetch();
Each time Fetch() is called, a randomly sorted list must be returned. The problem is, seed = r.nextInt()
won't be called each time I call Fetch(). How do I ensure that a new seed is generated every time Fetch()
is called?