slamsal
slamsal

Reputation: 620

Convert SQL to Linq where there is in clause

I would like to know how to convert SQL query listed below into LINQ query in VB.

SELECT FacilityID 
FROM tblFacilityProjects 
WHERE FreedomEnabled = True and ProjectID in (840,841,842)

Upvotes: 4

Views: 8639

Answers (3)

Tabish Sarwar
Tabish Sarwar

Reputation: 1525

Create a list of your project ID

Dim list As New List(Of Integer)
list.Add(840)
list.Add(841)
list.Add(842)

Now you have to use this in LINQ.

dim facilityId = from fp in tblFacilityProjects 
                 where FreedomEnabled == true
                 where list.Contains(ProjectID)
                 select new
                 {
                   FacilityID=fp.FacilityID
                 }

There must be some tweaks you may need to do for VB.net .

Upvotes: 0

mellamokb
mellamokb

Reputation: 56769

Something like this should do it I think:

Dim projectIds = New Integer() {840, 841, 842}
Dim result = From proj In db.tblFacilityProjects _
             Where proj.FreedomEnabled = True AndAlso _
                   projectIds.Contains(proj.ProjectID) _
             Select FacilityID

Upvotes: 4

Robert Harvey
Robert Harvey

Reputation: 180808

IN Query in SQL

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

IN Query in LINQ

Makes use of the Contains function of VB.

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

http://www.c-sharpcorner.com/blogs/4479/sql-in-and-not-in-clause-in-linq-using-vb-net.aspx

Upvotes: 0

Related Questions