c# - Random row from Linq to Sql with no duplicate -


what best (and fastest) way retreive random rows using linq sql unique data / no duplicate record? oh preffer in 1 statement, possible? found relevant question don't think approach resulting unique records.

i have tried far :

//first approach  airasiadatacontext linqdatactx = new airasiadatacontext(); var tes = (from u in linqdatactx.users.asenumerable()            orderby guid.newguid()            select u).take(5);    //second approach  var usr = u in linqdatactx.users           select u;   int count = usr.count(); // 1st round-trip  int index = new random().next(count);   list<user> tes2 = new list<user>();  (int = 0; < 5; i++)  {       tes2.add(usr.skip(index).firstordefault()); // 2nd round-trip      } 

as can see above, have tried 2 solution, works, above codes did not resulting unique records, there chances duplicate.

db.tablename.orderby(x=>guid.newguid()).firstordefault(); 

if want take unique data / no duplicate record, you'd better use list store row taked already.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -