-1

How to get Items of randomly selected using GUID? I tried it with the following method but do not sorted?

var Query1 = (from _factor in db.tblFactors orderby Guid.NewGuid() select _factor)
     .Take(5);
var Query2 = (db.tblFactors
    .Select(x => new { x.ID, x.Cost, x.UserID, fldOrder = Guid.NewGuid() })
        .OrderBy(x => x.fldOrder))
        .Take(5);

Of course the problem here is two:

-One is that why my code is not working

-Second, does this method for random selection using the GUID is true or not

Rob
  • 26,989
  • 16
  • 82
  • 98
  • For question 2 check http://stackoverflow.com/questions/467271/how-random-is-system-guid-newguid – w.b Feb 04 '16 at 16:48
  • Thank you But it did not help to solve the problem – Seyed M.H Mosavi Feb 04 '16 at 16:55
  • I fail to see anything random about this due to the GUID generation creating fairly ordered results (close to same but not exactly, they will have a lot of commonality) - perhaps put a real random number in and then order by that would be a better strategy here? As it is, I would suspect a high probability for the first or last 5 rows in the set. – Mark Schultheiss Feb 04 '16 at 17:10
  • Based upon that, the questions are somewhat unanswerable here due to the GUID constriction you envoke – Mark Schultheiss Feb 04 '16 at 17:14
  • Thank you very much, but this question was asked of me and I thought his answer that question! So how to get randomly select of List !? – Seyed M.H Mosavi Feb 04 '16 at 17:20
  • var NewQuery = db.tblFactors.ToList().OrderBy(x => rnd.Next()).Take(5); – Seyed M.H Mosavi Feb 04 '16 at 17:31

1 Answers1

0
List<tblFactor> _factor = db.tblFactors.ToList();
_factor = _factor.OrderBy(o => Guid.NewGuid()).ToList();
_factor = _factor.Take(5).ToList();
lst1.DataSource = _factor;
lst1.DataBind();

5 rows Selected randomly from the list using GUID

fixed!