SQL IN and NOT IN Clause in LINQ using VB.NET

This blog defines the IN and NOT IN clauses in SQL but in LINQ to achieve this task use Contains function of VB.NET.

IN clause

The IN clauses is used to filter the record from a list of records. The IN function helps reduce the need to use multiple OR conditions.

IN Query in SQL

SELECT [Id], [UserId]
FROM [Tablename)
WHERE [UserId] IN (2, 3)

IN Query in LINQ

The LINQ query to achieve this task makes use of the Contains function of Visual Basic(VB.NET).

Dim coll As Integer() = {2, 3}
Dim user = From u In tablename Where coll.Contains(u.UserId.Value)New From                                       {
u.id, u.userid
}

NOT IN clause

The NOT IN clauses is the opposed to the values that you do want.

NOT IN clause in SQL

SELECT [Id], [UserId]
FROM [tablename]
WHERE [UserId] NOT IN (2, 3)

NOT IN clause in LINQ

The LINQ query to achieve this task makes use of the Contains function of Visual Basic(VB.NET).

Dim coll As Integer() = {2, 3}
Dim user = From u In tablename Where Not coll.Contains(u.UserId.Value)u