0

Im trying to learn mvc and im stuck on something silly.

i have a list of int's which are the PK for a domain model. I want to query the db to get all the objects that have their pk's in the list. I cant fiugre out the code to query the db and searching brings all kinds of irellevent answers about drop down lists. Can anyone help?

something like this but this is wrong.

 List<User> userList = db.Users.Where(x => x.UserID == idList);

EDIT The error im getting is this.

Operator '==' cannot be applied to operands of type 'int' and 'System.Collections.Generic.List'

2 Answers 2

1

Try this:

List<User> userList = db.Users.Where(x => idList.Contains(x.UserID)).ToList();

This will translate to the SQL in operator.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks for your time. That looks more like what im trying to do. but i get the error Error 1 Cannot implicitly convert type 'System.Linq.IQueryable<Quit.Models.User>' to 'System.Collections.Generic.List<Quit.Models.User>'. An explicit conversion exists (are you missing a cast?)
Right. Add a .ToList() at the end.
Urgh its been a long day, sorry. Thanks!
0

The above looks correct. Have you instantiated your db?

For example: DBContext db = new DBContext;

Comments

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.