Is there anyway to completely randomize the result set like sql server's "order by newid()" trick?
2 Answers, 1 is accepted
0
Petko_I
Telerik team
answered on 15 Sep 2010, 05:09 PM
Hello Jay,
The randomization in a LINQ query cannot be propagated to the database server with OpenAccess. However, if you retrieve the persistent objects in memory, you can benefit from the LINQ to Objects implementation. You can then order your items by a random number generated with the Random class.
var orderedQuery = query.ToList().OrderBy(x => random.Next());
foreach(var category inorderedQuery)
{
Console.WriteLine(category.CategoryName);
}
}
There is another option which is situationally useful – you can create a view which performs the ordering on the database server side and map it to a persistent type. Each time you retrieve a collection of persistent objects from this view, you will get them in a random order unless you order them explicitly.
I hope this helps. Do not hesitate to contact us if you have more questions.
Regards,
Petko_I
the Telerik team
Do you want to have your say when we set our development plans?
Do you want to know when a feature you care about is added or when a bug fixed?
Explore the
Telerik Public Issue Tracking
system and vote to affect the priority of the items