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
  • 7,015
  • 17
  • 72
  • 126
  • 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));
ocuenca
  • 38,548
  • 11
  • 89
  • 102
4

something like :

context.avatars.Where(a => new[] { 1, 2, 3 }.Contains(a.userId));
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122