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
Post a Comment