Skip to main content

LINQ To SQL Samples - NULL


Null

This sample uses the null value to find Employees that do not report to another Employee.

Public Sub LinqToSqlNull01()
    Dim q = From e In db.Employees _
            Where e.ReportsTo Is Nothing

    ObjectDumper.Write(q)
End Sub

Result:
EmployeeID=2    LastName=Fuller         FirstName=Andrew        Title=Vice President, Sales     TitleOfCourtesy=Dr.     BirthDate=2/19/1952     HireDate=8/14/1992      Address=908 W. Capital Way      City=Tacoma     Region=WA       PostalCode=98401        Country=USA     HomePhone=(206) 555-9482        Extension=3457  Photo=...       Notes=Andrew received his BTS commercial in 1974 and a Ph.D. in international marketing from the University of Dallas in 1981.  He is fluent in French and Italian and reads German.  He joined the company as a sales representative, was promoted to sales manager in January 1992 and to vice president of sales in March 1993.  Andrew is a member of the Sales Management Roundtable, the Seattle Chamber of Commerce, and the Pacific Rim Importers Association.  ReportsTo=null  PhotoPath=http://accweb/emmployees/fuller.bmp   Orders=...      EmployeeTerritories=...         Employees=...   Employee={ }


Nullable(Of T).HasValue

This sample uses Nullable(Of T).HasValue to find Employees that do not report to another Employee.

Public Sub LinqToSqlNull02()
    Dim q = From e In db.Employees _
        Where Not e.ReportsTo.HasValue _
        Select e

    ObjectDumper.Write(q)
End Sub

Result:
EmployeeID=2    LastName=Fuller         FirstName=Andrew        Title=Vice President, Sales     TitleOfCourtesy=Dr.     BirthDate=2/19/1952     HireDate=8/14/1992      Address=908 W. Capital Way      City=Tacoma     Region=WA       PostalCode=98401        Country=USA     HomePhone=(206) 555-9482        Extension=3457  Photo=...       Notes=Andrew received his BTS commercial in 1974 and a Ph.D. in international marketing from the University of Dallas in 1981.  He is fluent in French and Italian and reads German.  He joined the company as a sales representative, was promoted to sales manager in January 1992 and to vice president of sales in March 1993.  Andrew is a member of the Sales Management Roundtable, the Seattle Chamber of Commerce, and the Pacific Rim Importers Association.  ReportsTo=null  PhotoPath=http://accweb/emmployees/fuller.bmp   Orders=...      EmployeeTerritories=...         Employees=...   Employee={ }


Nullable(Of T).Value

This sample uses Nullable(Of T).Value for Employees that report to another Employee to return the EmployeeID number of that employee. Note that the .Value is optional.

Public Sub LinqToSqlNull03()
    Dim q = From e In db.Employees _
        Where e.ReportsTo.HasValue _
        Select e.FirstName, e.LastName, ReportsTo = e.ReportsTo.Value

    ObjectDumper.Write(q)
End Sub

Result:
FirstName=Nancy         LastName=Davolio        ReportsTo=2
FirstName=Janet         LastName=Leverling      ReportsTo=2
FirstName=Margaret      LastName=Peacock        ReportsTo=2
FirstName=Steven        LastName=Buchanan       ReportsTo=2
FirstName=Michael       LastName=Suyama         ReportsTo=5
FirstName=Robert        LastName=King   ReportsTo=5
FirstName=Laura         LastName=Callahan       ReportsTo=2
FirstName=Anne  LastName=Dodsworth      ReportsTo=5