views:

49

answers:

1

Hello,

How can I retreive rows from table only when UserId is in my Users list ?

Code below doesnt work :/

List<int> selectedSourceUsers = ...
MyModelDataContext context = ...

e.Result = from u in context.Users
                   from id in selectedSourceUsers
                   where u.UserId == id
                   select u;

Thanks for help

+3  A: 

Try this:

int[] selectedUsersArray = selectedSourceUsers.ToArray();
e.Result = from u in context.Users
           where selectedUsersArray.Contains(u.UserId)
           select u;

(To be honest I'd have expected it to work with a List<int> as well, but using an array instead may fix it...)

Jon Skeet
doesnt work:No translation for SQL Language for method „Boolean Contains(Int32)”.I translated error from polish so it may sound different in original verison
gruber
@snorlaks: Try converting the list to an `int[]` first. I'd expect that to work... I'll edit the answer to make this clearer.
Jon Skeet
Now it works but htere is an error:You cant assign null value to member of type System.Int32 which is of type not nullable or sth like that :)
gruber
Sorry :)It works correctly, my mistake was by calling ToList() method:from u in context.Users.ToList()I dont know why I have to create array to do such a simple task :/
gruber