none
Linq statement to check contents of a property only if that property is not nothing RRS feed

  • Question

  • I am wondering if I can put the following two statements into one.

            Dim result = (From gs As GameSquare In bigList Where (gs.theGameItem IsNot Nothing) Select gs).ToList
            Dim res2 = (From gs In result Where (gs.theGameItem.GetType() = GetType(GameHut)) Select gs).ToList()
    

    If I try to put them into one statement I get a null exception when I try to check if the property is of a certain type since that property is null/nothing.

    Dim result2 = (From gs As GameSquare In bigList Where (gs.theGameItem IsNot Nothing) And (gs.theGameItem.GetType() = GetType(GameHut)) Select gs).ToList
    The two statements work but since I will be using many of these statements in my app it would be nice if I only had one statement for readability.


    Lloyd Sheen

    Wednesday, February 7, 2018 3:28 PM

Answers

  • Loyd,

    I did not look at your sentence in dept. But use in VB for .Net always AndAlso if it is not a Boolean operator. 

    It was a strange genuflection to the angry VB6 MVP's when they made the And equal to the Short circuited && in C# 


    Success

    Cor



    • Edited by Cor Ligthert Wednesday, February 7, 2018 5:02 PM
    • Marked as answer by sqlguy Wednesday, February 7, 2018 5:12 PM
    Wednesday, February 7, 2018 4:59 PM

All replies

  • Loyd,

    I did not look at your sentence in dept. But use in VB for .Net always AndAlso if it is not a Boolean operator. 

    It was a strange genuflection to the angry VB6 MVP's when they made the And equal to the Short circuited && in C# 


    Success

    Cor



    • Edited by Cor Ligthert Wednesday, February 7, 2018 5:02 PM
    • Marked as answer by sqlguy Wednesday, February 7, 2018 5:12 PM
    Wednesday, February 7, 2018 4:59 PM
  • Thanks Cor

    Lloyd Sheen

    Wednesday, February 7, 2018 5:12 PM