none
Test for nothing within lambda expression RRS feed

  • Question

  • I have the following expression:

    Dim bResult As Boolean = (From x In myList Where x.PopulationCriteriaId = 20 Select x.Result1).FirstOrDefault.ToLower = "true"

    Result1 is a string value that will contain "true" or "false".  The problem is, result1 may or may not exist in the list. If it doesn't exist, I get an object reference not set error. Is there any way to test for existence within the lambda expression and avoid the error?

    If it doesn't exist, the boolean should be false.

    Monday, August 8, 2016 6:58 PM

Answers

  • Hi Eregnon the Black,

    I would suggest assign the result to a variable, according to result. we could use the following code to implement it.

    Dim result = (From x In myList Where x.PopulationCriteriaId = 22 Select x.Result1).FirstOrDefault
    Dim bResult As Boolean = If(result Is Nothing, False, result.ToLower() = "true")

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 9, 2016 1:46 AM
    Moderator
  • Try this new feature too:

    Dim bResult As Boolean = (From x In myList Where x.PopulationCriteriaId = 20 Select x.Result1).FirstOrDefault?.ToLower = "true"

    Also check this:

    Dim bResult As Boolean = myList.Any(Function(x) x.PopulationCriteriaId = 20 And "true".Equals(x.Result1, StringComparison.InvariantCultureIgnoreCase))
    
    

    If possible, maybe consider changing the type of Result1 in the list from String to Boolean.



    Tuesday, August 9, 2016 5:29 AM

All replies

  • Hi Eregnon the Black,

    I would suggest assign the result to a variable, according to result. we could use the following code to implement it.

    Dim result = (From x In myList Where x.PopulationCriteriaId = 22 Select x.Result1).FirstOrDefault
    Dim bResult As Boolean = If(result Is Nothing, False, result.ToLower() = "true")

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 9, 2016 1:46 AM
    Moderator
  • Try this new feature too:

    Dim bResult As Boolean = (From x In myList Where x.PopulationCriteriaId = 20 Select x.Result1).FirstOrDefault?.ToLower = "true"

    Also check this:

    Dim bResult As Boolean = myList.Any(Function(x) x.PopulationCriteriaId = 20 And "true".Equals(x.Result1, StringComparison.InvariantCultureIgnoreCase))
    
    

    If possible, maybe consider changing the type of Result1 in the list from String to Boolean.



    Tuesday, August 9, 2016 5:29 AM