locked
Linq FirstOrDefault Null Exception Handling RRS feed

  • Question

  • Hallo,

    I have a class equipment and it has a custom object "Type" and Type has "Material". If Type does not have any material then i get null exception. How do i handle it. If type has material then it works without any problem.

     Dim str_MID As String = EQIP.Item(vt_cbx_eq.SelectedIndex).Type.ToList.Item(0).Material.FirstOrDefault.MID.ToString
    Thanks

    Tuesday, October 6, 2020 4:35 PM

Answers

  • It's best to break the logic apart rather than do it all in one line if there is a chance for a null/nothing return value.

    One might use

    Public Shared Function CategoryName(categoryIdentifier As Integer) As String
    
        Using context As New NorthWindContext
            Return context.Categories.FirstOrDefault(Function(cat) cat.CategoryID = categoryIdentifier)?.CategoryName
        End Using
    
    End Function


    But I would suggest

    Public Shared Function GetCategory(categoryIdentifier As Integer) As Category
    
        Using context As New NorthWindContext
            Return context.Categories.FirstOrDefault(Function(cat) cat.CategoryID = categoryIdentifier)
        End Using
    
    End Function

    Then check for Nothing, if not nothing access CategoryName


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, October 6, 2020 5:06 PM
  • Try this too:

    Dim str_MID As String = EQIP.Item(vt_cbx_eq.SelectedIndex).Type.ToList.Item(0).Material?.FirstOrDefault?.MID?.ToString

    (Maybe ToList.Item(0) can be replaced with First).

    • Marked as answer by Shan1986 Tuesday, October 6, 2020 7:04 PM
    Tuesday, October 6, 2020 6:26 PM

All replies

  • It's best to break the logic apart rather than do it all in one line if there is a chance for a null/nothing return value.

    One might use

    Public Shared Function CategoryName(categoryIdentifier As Integer) As String
    
        Using context As New NorthWindContext
            Return context.Categories.FirstOrDefault(Function(cat) cat.CategoryID = categoryIdentifier)?.CategoryName
        End Using
    
    End Function


    But I would suggest

    Public Shared Function GetCategory(categoryIdentifier As Integer) As Category
    
        Using context As New NorthWindContext
            Return context.Categories.FirstOrDefault(Function(cat) cat.CategoryID = categoryIdentifier)
        End Using
    
    End Function

    Then check for Nothing, if not nothing access CategoryName


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, October 6, 2020 5:06 PM
  • Try this too:

    Dim str_MID As String = EQIP.Item(vt_cbx_eq.SelectedIndex).Type.ToList.Item(0).Material?.FirstOrDefault?.MID?.ToString

    (Maybe ToList.Item(0) can be replaced with First).

    • Marked as answer by Shan1986 Tuesday, October 6, 2020 7:04 PM
    Tuesday, October 6, 2020 6:26 PM
  • Thanks Karen and Viorel , both works and thanks for the tip "ToList.Item(0) can be replaced with First"
    Tuesday, October 6, 2020 7:06 PM