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