1

I have a list of user ids and want to get a few random users from a database which doesn't contain users from my list using LINQ-SQL.

For example:

//it's user ids 
var existsUsers = new[]{1,2,3,4}

// I want to implement this function:
List<User> users = GetRandomUsers(randomUsersCount, existsUsers)
sth
  • 222,467
  • 53
  • 283
  • 367
Neir0
  • 12,849
  • 28
  • 83
  • 139
  • 2
    And what is your question? What have you tried / done / achieved until now? What's the data model you want to access? – Tomas Narros Mar 24 '11 at 12:17
  • I think the question is pretty clear. He wants to know how to do something like `allUsers.Except(existsUsers).Shuffle().Take(randomUsersCount)` in linq-to-sql. – CodesInChaos Mar 24 '11 at 12:21
  • It is not clear whether he has problems with some specific part of it or he wants us to write the entire code. – Snowbear Mar 24 '11 at 12:27
  • See: [Random row from Linq to Sql](http://stackoverflow.com/questions/648196/random-row-from-linq-to-sql) – Ani Mar 24 '11 at 12:27
  • @Tomas Narros: Data model is just User{int id, varchar Name}. I can get ALL users and then filter them. But i think it's not good because of perfomance problems. – Neir0 Mar 24 '11 at 12:28
  • @Ani Thanks but that code just get a random users. I want to get random users with some conditions. – Neir0 Mar 24 '11 at 12:31
  • How large is your exception list compared to the total number of users in the db? – CodesInChaos Mar 24 '11 at 12:31

1 Answers1

1

Seems that you just can do an Linq In Clause since you have the ids already.

//Not tested.... may have syntax errors.
GetRandomUsers(randomUsersCount, existsUsers)
{
     var users= (from u in users
                where existsUsers.Contains(u.Id)
                select u).Take(randomUserCount);

     return users;

}
Community
  • 1
  • 1
Giovanni Galbo
  • 12,963
  • 13
  • 59
  • 78