Subscribe to our YouTube Channel for more Videos

LINQ Equivalent of SQL’s “IN” keyword

I came across the requirement to use the “IN” equivalent of LINQ when using Entity Framework . I am sure most of the .NET Developers would also have come across a situation to use “IN” in LINQ .

Here’s an example  .

The SQL Query looks like this

idiots)','Pagalvan','Yohan  Adhyayam Ondru','Maalai Neram

When using LINQ , We can use the Contains Keyword which is the equivalent of “IN” keyword in SQL .

Below is the sample using LINQ Query

public partial class Form1 : Form
   public Form1()

   private void Form1_Load(object sender, EventArgs e)
     string[] upcoming = new string[] { "Velayutham", "Nanban",
     "Pagalvan","Yohan  Adhyayam Ondru","Maalai Neram Mazhaithuli" };
     List<Movie> Movies = new List<Movie>();
     Movies.Add(new Movie { Name = "Ghilli", Director = "Dharani" });
     Movies.Add(new Movie { Name = "Kaavalan", Director = "Siddique" });
     Movies.Add(new Movie{ Name ="Velayutham" , Director ="Jayam Raja" });
     Movies.Add(new Movie{ Name ="Pagalvan" , Director = "Seeman" });
     Movies.Add(new Movie{ Name ="Yohan " , Director = "Gautham Menon" });
     var UpcomingMovies = (from m in Movies where upcoming.Contains(m.Name)
                            select m ).ToList();

public class Movie

     public string Name { get; set; }

     public string Director { get; set; }

Using Lambda Expressions

var UpcomingMoviesByLambda =     Movies.Where(c => upcoming.Contains(c.Name));

What is Entity Framework ?

Know more about Entity Framework from MSDN

Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.