Results 1 to 9 of 9

Thread: [RESOLVED] LINQ problem

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Resolved [RESOLVED] LINQ problem

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim v1 = (From x In "abcd" Where x = "a" Select x).FirstOrDefault
        Dim v2 = (From x In "abcd" Where x = "m" Select x).FirstOrDefault
        Dim v3 = (From x In "abcd" Where x = "a" Select x).FirstOrDefault
        Dim v4 = (From x In "abcd" Where x = "a" Select x).FirstOrDefault
        Dim result = Nz(v1, "-") & Nz(v2, "-") & Nz(v3, "-") & Nz(v4, "-")
        MsgBox(result)
    End Sub
    
    Function Nz(ByVal var As String, ByVal substitiution As String) As String
        Return If(var IsNot Nothing, var, substitiution)
    End Function
    I was expecting it to produce "a-aa", but the result is "a".
    Any ideas why?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: LINQ problem

    It's all about data types, and this is a prime example of why it's important to be aware of what data types you're dealing with. In your LINQ query, your range variable 'x' is NOT type String, but rather type Char. FirstOrDefault will return Nothing if there are no items in the sequence, and Nothing cast as type Char is a null character, i.e. ControlChars.Null.

    When you pass that to your 'var' parameter, which is type String, it doesn't become Nothing because it is a Char, so you end up with a String containing one Char. That means that your 'result' variable ends up containing a null Char rather than a dash.

    Now, when you display that, the null Char is interpreted as a string terminator, so only those characters before it, i.e. the one 'a', get displayed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: LINQ problem

    Thank you so much

    So specifying it as String corrects the problem:
    Code:
    Dim v1 = (From x As String In "abcd" Where x = "a" Select x).FirstOrDefault
    Dim v2 = (From x As String In "abcd" Where x = "m" Select x).FirstOrDefault
    Dim v3 = (From x As String In "abcd" Where x = "a" Select x).FirstOrDefault
    Dim v4 = (From x As String In "abcd" Where x = "a" Select x).FirstOrDefault
    Dim result = Nz(v1, "-") & Nz(v2, "-") & Nz(v3, "-") & Nz(v4, "-")
    MsgBox(result)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: LINQ problem

    and Nothing cast as type Char is a null character, i.e. ControlChars.Null.
    This is what I learnt new today
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: LINQ problem

    Should have been NullChar rather than just Null.

    Remember that Char is a value type so a Char variable must always have a value. Nothing is a stack value containing zero and that zero is interpreted differently depending on the type. For a reference type it is a null reference, for a numeric type it is the number zero and for type Char it is a null character.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: LINQ problem

    yes.. right.

    When I moved my mouse over variable v2, it shows "Nothing" there. And that's what confuses. It could show something better based on what it contains.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: LINQ problem

    Quote Originally Posted by Pradeep1210 View Post
    yes.. right.

    When I moved my mouse over variable v2, it shows "Nothing" there. And that's what confuses. It could show something better based on what it contains.
    Apart from Nothing, the only really acceptable option would be '\0' which is the C-style escape sequence for a null character. Wouldn't really mean much to VB developers.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: LINQ problem

    Usually it shows a square block for characters that can't be shown on the screen. I guess that could have been a better option than showing Nothing. At-least the developer would know that there is something there, instead of assuming that it is Nothing (i.e. no object).
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [RESOLVED] LINQ problem

    I can't rep you due to forum restrictions, but can certainly express my thanks here.
    Thank you for making me learn something new today.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width