2015-06-11 97 views
0

我有数据库。其实,我正在为它从我的数据库显示最新的10个图像,但我想从数据库中显示随机10图像。我该怎么做?从mvc4中的数据库中选择随机数据C#

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

为什么你就不能在现有的指数范围内产生10张随机数(0 - >块长度)? http://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c – bill

回答

4

这样做:

(from p in products orderby Guid.NewGuid() select p).Take(10).ToList() 
+0

我喜欢'orderby Guid.NewGuid()'随机。我用了很多。 – ManOVision

+0

非常感谢,我试过了,效果很好! – doduc812