0

I want my search to return Random entries from database.How to do this?
This is my search function where i am using Join query and have taken a new model to pass Attributes.

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());
Neeraj Mehta
  • 1,675
  • 2
  • 22
  • 45
  • possible duplicate of [Random row from Linq to Sql](http://stackoverflow.com/questions/648196/random-row-from-linq-to-sql) – Magnus Oct 05 '14 at 10:34
  • possible duplicate of [EF Code First: How to get random rows](http://stackoverflow.com/questions/7781893/ef-code-first-how-to-get-random-rows) – Gert Arnold Oct 05 '14 at 11:30

1 Answers1

2

By inserting the guid (which is random) the order with orderby would be random.:

  planetfeedsOrder.OrderBy(c => Guid.NewGuid()).Take(itemCount).ToList()
Vlado Pandžić
  • 4,879
  • 8
  • 44
  • 75