|
-
Jan 24th, 2011, 05:13 AM
#1
[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?
-
Jan 24th, 2011, 05:41 AM
#2
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.
-
Jan 24th, 2011, 06:55 AM
#3
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)
-
Jan 24th, 2011, 08:38 AM
#4
Re: LINQ problem
and Nothing cast as type Char is a null character, i.e. ControlChars.Null.
This is what I learnt new today
-
Jan 24th, 2011, 08:55 AM
#5
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.
-
Jan 24th, 2011, 09:10 AM
#6
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.
-
Jan 24th, 2011, 09:15 AM
#7
Re: LINQ problem
 Originally Posted by Pradeep1210
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.
-
Jan 24th, 2011, 09:57 AM
#8
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).
-
Jan 24th, 2011, 01:59 PM
#9
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|