i know of using the
ORDER BY NEWID()
feature when running a regular sql command. I’m wanting to do the same thing, but via linq2sql.
I’d prefer not to select the whole range, add in a random number via rnd.Next(), and then sort by that…
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Marc Gravell posted a solution here that allows you to define a function that uses NEWID in a partial class for the DataContext. Don’t place it in the generated DataContext class otherwise future updates would wipe out what you’ve added.
Marc shows how to use it with the query expression syntax. Alternately, with dot notation, you could write:
In case you’re not familiar with creating a partial class, just add a new class to your solution. Its name doesn’t matter as long as the class definition uses the DataContext class name with the
partialkeyword. For example, if your DataContext is namedXYZDataContextyou can add a new class namedXYZDataContextPartial.csand define it as: