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?

1

2 Answers 2

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();
}    
Sign up to request clarification or add additional context in comments.

Comments

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
}

4 Comments

If this is a dupe, don't post the link as an answer...post a comment instead.
dont know how to vote do close.. maybe dont have permission yet?
its not the dublicate, im a new in mvc and i realy dont understand how to make in by that example
updated.. maybe you can speed that up by using 10 skips with random based on count/10.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.