0

Try to find the solution but i cant.

So problem is next one. I have the EDM model of database. I have a class with functions to get data from DB. Like this:

public IQueryable<photos> FindUserPhotos(string userlogin)
        {
            return from m in db.photos
                   where m.userlogin == userlogin
                   select m;
        }

How to get the Random 10 lines from DB?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Evgeniy Labunskiy
  • 2,012
  • 3
  • 27
  • 45

2 Answers2

4

I always use this method for get custom entity OrderBy(x => Guid.NewGuid())

public photos Find10RandomUserPhotos(string userlogin)
{
   return db.photos.Where(x => x.userlogin == userlogin).OrderBy(x => Guid.NewGuid()).Take(10).ToList();
}    
İbrahim Özbölük
  • 3,162
  • 23
  • 20
0

Following Random row from Linq to Sql

public photos FindRandomUserPhoto(string userlogin)
{
   var qry = FindUserPhotos(userlogin);
   int count = qry.Count();
   int index = new Random().Next(count);
   return qry.Skip(index).FirstOrDefault();
}

public Array<photos> Find10RandomUserPhotos(string userlogin)
{
   var result = New Array<photos>;
   for (i = 0; i < 10; i++) {
      result.add(FindRandomUserPhoto(userlogin));
   }
   return result
}
Community
  • 1
  • 1
ariel
  • 15,620
  • 12
  • 61
  • 73