0

Beginner level programmer here and I've been stuck with this issue of querying my database (SQL Server) through Microsoft Visual Studio and actually returning the tuple of the result I want.

For instance.. If I want the sector of a particular Project ID, I'd query it like this in a subroutine, and call the subroutine

Public Sub testing()
    projectID = 1

    Dim sqlQueryObject = (From a In db.Project1 Where a.ProjectID = projectID
                          Select a.Sector)   'Using LINQ

    'Now have the object (works fine)
    'The Issue is getting the actual sector value from this query...in which I've tried a couple of things

End Sub
  1. dim somestring as string = Cstr(sqlQueryObject)
  2. dim somestring as string = Ctype(sqlqueryobject, string)

Neither work, If the sector for projectID 1 is named Zero - How do I program it to return this value?

Jesse
  • 8,605
  • 7
  • 47
  • 57

1 Answers1

0

The LINQ query you have given will return a list containing the elements that meet the criteria in your Where clause. For the query to only return a single value you need to apply either .Single(), .SingleOrDefault(), .First() or .FirstOrDefault() to the query.

For example the following would return either a single scalar value or the default value of the type being returned.

Dim sector = (From a In db.Project1 
              Where a.ProjectID = projectID
              Select a.Sector
             ).SingleOrDefault() 

Further information about these methods can be found in this question.

Community
  • 1
  • 1
mickfold
  • 2,003
  • 1
  • 14
  • 20