2015-06-11 98 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