2014-10-05 42 views
0

我想要我的搜索从数据库中返回随机条目。如何执行此操作?
这是我的搜索功能,我正在使用Join查询并已采用新模型来传递属性。如何使用Linq从mvc4的数据库中获得随机条目

var planetfeedsOrder = from a in db.PlanetFeeds 
         where a.PlanetFeedOwnerId == id || a.PlanetFeedPosterId == id 
         && a.CurrentState != 1 
         join c in db.Graphs on a.PlanetFeedItemGraphId equals c.GraphID 
         join u in db.UserInfos on a.PlanetFeedOwnerId equals u.UserInfoID 
         orderby a.PostDate descending 
         select new UserInfoViewModel 
         { 
         AvatarURL = u.AvatarURL, 
         UserName=u.FirstName +" "+u.LastName, 
         GraphItemDescription = c.GraphItemDescription, 
         GraphItemURL = c.GraphItemURL, 
         isRootFeed = a.isRootFeed, 
         PostDate = a.PostDate, 
         CurrentState = a.CurrentState, 
         };      
return PartialView("_PlanetfeedPartial",planetfeedsOrder.Take(itemCount).ToList()); 
+0

可能重复http://stackoverflow.com/questions/648196/random-row-from-linq-to-sql) – Magnus 2014-10-05 10:34:27

+0

[EF Code First:如何获得随机行](http://stackoverflow.com/questions/7781893/ef-code-first - 如何对获得随机-行) – 2014-10-05 11:30:41

回答

2

通过将GUID(这是随机的)与排序依据的顺序将是随机的。:

planetfeedsOrder.OrderBy(c => Guid.NewGuid()).Take(itemCount).ToList() 
[从Sql的Linq随机行(的
相关问题