0

I have database. Actually,i am doing for it display 10 image newest from my database but i want to display random 10 image from database.How can i do it?

public ActionResult Details(int id)
    {
        var products = db.TblProductImages.Include(t => t.TblProduct);
        ViewData["viewcategory"] = (from p in products orderby  p.ProductID   descending select p).Take(10).ToList();
        if (image == null)
        {
            return HttpNotFound();
        }
        return View(image);
    }
doduc812
  • 3
  • 2
  • 1
    Why can't you just generate 10 random numbers in the range of the available index (0 -> db Length)? http://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c – bill Jun 11 '15 at 16:15

1 Answers1

4

Do this:

(from p in products orderby  Guid.NewGuid() select p).Take(10).ToList()
Alioza
  • 1,690
  • 12
  • 14