0

How do I use the IN keyword under Code First?

SELECT * FROM table1 WHERE id IN (1,2,3);

Been looking around google and can't find anything unless the only way is raw sql.

justadeveloper
  • 86
  • 4
  • 12
  • what you are getting while executing the query – sujith karivelil Sep 01 '15 at 09:05
  • with the help of contains `var val = tblName.Where(t => col1.Contains(paramName));` – wiretext Sep 01 '15 at 09:07
  • Note sure what "code first" has to do with anything but the linq equivalent `Where(x => ArrayOfValues.Contains(x.Id))` whill be translated to `WHERE IN` type sql – Ben Robinson Sep 01 '15 at 09:07
  • I haven't executed the query 'cause I don't know how I will write the query in where of c#.. but I'll try what @haim770 have commented – justadeveloper Sep 01 '15 at 09:09
  • I've passed an array of int and receiving error `Unable to create a null constant value of type 'System.Int32[]'. Only entity types, enumeration types or primitive types are supported in this context.` – justadeveloper Sep 01 '15 at 09:19
  • @juniordeveloper, Please update the question with what you have tried – haim770 Sep 01 '15 at 09:50
  • @juniordeveloper, The last exception you mentioned is probably because you're creating the `int[]` inside the expression. Try to instantiate it before, just like in the other answer. – haim770 Sep 01 '15 at 09:56
  • thanks, just solved the problem, I'm passing an array from js and it's being received as a string array.. will update my question now... how do I mark my question as already answered on the duplicate? – justadeveloper Sep 01 '15 at 09:58

1 Answers1

0

This answer should help: https://stackoverflow.com/a/13342905/2892208

EF should decide how he render the script but I believe the .Contains method will fit here perfectly.

Community
  • 1
  • 1
jgasiorowski
  • 1,033
  • 10
  • 20