tags:

views:

45

answers:

4
from f in db.Table1
orderby Guid.NewGuid()
select f

this doesn't seem to work. how can i randomize results?

+1  A: 

How about

SELECT TOP 1 column FROM table ORDER BY NEWID and skip the linq :)

Or try this:

var t = (from row in db.Table1 order by table1.random()
         select row).FirstOrDefault();
JonH
there's no random()
proGress
A: 

Maybe something like this works (not tested):

 (from f in db.Table1 select new { f, r = Guid.NewGuid()}).OrderBy(x => x.r)
despart
A: 

Randomize whole list

db.Table1.OrderBy(x => Guid.NewGuid())

Get single Random

db.Table1.OrderBy(x => Guid.NewGuid()).FirstOrDefault();
masenkablast
doesnt work....
proGress
A: 

I like to write an extension method for this.

IEnumerable<T> Randomize(this IEnumerable<T> list)
{
    T[] result = list.ToArray();
    Random random = new Random();

    for(int i = result.Length; i > 0; i--)
    {
        result[i] = random.Next(i);
    }

    return (result);
}
NickLarsen