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);
    }
1
  • 1
    Why can't you just generate 10 random numbers in the range of the available index (0 -> db Length)? stackoverflow.com/questions/2706500/… Commented Jun 11, 2015 at 16:15

1 Answer 1

4

Do this:

(from p in products orderby  Guid.NewGuid() select p).Take(10).ToList()
Sign up to request clarification or add additional context in comments.

1 Comment

I love orderby Guid.NewGuid() for random. I use it a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.