1

I am trying to select from where in out of the database using Linq. The query I would be trying to reproduce is:

   "Select * From Avatars Where userId IN (1, 2, 3)"

How can this be written in lambda if I have a List of userIds.

I am stuck where the !! are and have a List of userIds:

context.avatars.Where(a => a.userId == !!(userIds)!! )
allencoded
  • 6,604
  • 17
  • 67
  • 117
  • http://stackoverflow.com/questions/194930/how-do-i-use-linq-containsstring-instead-of-containsstring – lazy Dec 31 '15 at 18:52

2 Answers2

5

You can use Contains method:

var result=context.avatars.Where(a => userIds.Contains(a.userId));

Or Any:

 var result=context.avatars.Where(a => userIds.Any(e=>a.userId==e));
octavioccl
  • 37,218
  • 8
  • 83
  • 98
4

something like :

context.avatars.Where(a => new[] { 1, 2, 3 }.Contains(a.userId));
Hatted Rooster
  • 34,596
  • 6
  • 59
  • 115