Retrieve records from database randomly in LINQ
-
Wednesday, August 08, 2007 4:58 AM
Is there a way to get a specific amount of record from a table randomly? like we always did in SQL
SELECT TOP 10 * FROM TABLE1 ORDER BY NEWID();
I tried to use order by Guid.Newid() but it didn't work"orderby" is only one step in a LINQ expression building series, like:
Code Snippetvar aaa = from c in ctx.ccc
select c;
swtich(circumstance1)
{
//add some filter
}
swtich(circumstance2)
{
//add some filter
}
swtich(ordertype)
{
//different order types including order by newid()
}
if I use ctx.ExecuteQuery, I nearly need to re-write all the codes in SQL statement is there anything like SqlMethod I can use?
All Replies
-
Wednesday, August 08, 2007 9:47 AM
Hello,
Have you looked at these posts:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1943994&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1768645&SiteID=1
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/06/15/9489.aspx
http://west-wind.com/WebLog/posts/33570.aspx
There a good starting point....if you have any more problems, let us know.
Thanks
Ben
-
Thursday, August 16, 2007 2:50 PMI'm curious as to whether a solution exists for the original question, i.e.:
Is there a way to get a specific amount of record from a table randomly? like we always did in SQL
SELECT TOP 10 * FROM TABLE1 ORDER BY NEWID();
My first thought was to to write a SQL function... but then you get an error when trying to use NEWID() in the function!
Joe

