4

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)
Ralph Shillington
  • 19,493
  • 20
  • 85
  • 150
slamsal
  • 590
  • 7
  • 13

3 Answers3

4

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
mellamokb
  • 53,762
  • 11
  • 101
  • 131
0

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

Robert Harvey
  • 168,684
  • 43
  • 314
  • 475
0

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 .

Tabish Sarwar
  • 1,427
  • 1
  • 11
  • 17