Search code examples
asp.netlinq-to-sqlrepeater

ORDER BY NEWID in LINQ and bind to Repeater control


Morning,

I would like to know how to write the following SQL statement in LINQ.

 SELECT TOP 6 * FROM Questions
 ORDER BY NEWID()

I would also like to know, how i could bind this to a asp.net repeater control to display the 6 questions.

Many thanks :)


Solution

  • The Linq style would be

     Questions.OrderBy(q=>Guid.NewGuid()).Take(6)
    

    then you attach that to a repeater by setting its DataSource property to the above, and calling the DataBind method.