[Q] LINQ to XML - NULL value in SELECT

GFR_2009

Member
Oct 9, 2009
37
0
0
Hi, I have the following code, where one element can be NULL.

Code:
 Select New Class_Data_Inventory With {.PARTS_AC = CDbl(Data_Inventory.Element("PARTS_AC").Value), _
                                               .PARTS_COMPANY = Data_Inventory.Element("PARTS_COMPANY").Value, _
                                               .PARTS_DIAM = CDbl(Data_Inventory.Element("PARTS_DIAM").Value), _
                                               [B][COLOR="Red"].PARTS_LENGTH = CDbl(If(Data_Inventory.Element("PARTS_LENGTH").Value Is Nothing, Data_Inventory.Element("PARTS_LENGTH").Value, "0")), [/COLOR][/B]_
                                               .PARTS_NAME = Data_Inventory.Element("PARTS_NAME").Value, _
                                               .PARTS_SD = CDbl(Data_Inventory.Element("PARTS_SD").Value), _
                                               .PARTS_STOCK = Data_Inventory.Element("PARTS_STOCK").Value, _
                                               .PARTS_TYPE = Data_Inventory.Element("PARTS_TYPE").Value}).ToList()
My question is, how can I handle the NULL value, and assing a 0 to that element?

So far, no luck!

In the code, I tried the IF clause, but doesn't work at all...

the .PARTS_LENGTH element is defined as DOUBLE and is the value I'd like to make 0 if it's NULL.

Any help is much appreciated in advance.
 

DaveShaw

Senior Moderator Emeritus + Wiki Bureaucrat
Dec 4, 2007
8,771
496
83
Huddersfield
www.taeguk.co.uk
Code:
 Select New Class_Data_Inventory With {.PARTS_AC = CDbl(Data_Inventory.Element("PARTS_AC").Value), _
                                               .PARTS_COMPANY = Data_Inventory.Element("PARTS_COMPANY").Value, _
                                               .PARTS_DIAM = CDbl(Data_Inventory.Element("PARTS_DIAM").Value), _
                                               [B][COLOR="Red"].PARTS_LENGTH = CDbl(If(Data_Inventory.Element("PARTS_LENGTH") Is Nothing, "0", Data_Inventory.Element("PARTS_LENGTH").Value)), [/COLOR][/B]_
                                               .PARTS_NAME = Data_Inventory.Element("PARTS_NAME").Value, _
                                               .PARTS_SD = CDbl(Data_Inventory.Element("PARTS_SD").Value), _
                                               .PARTS_STOCK = Data_Inventory.Element("PARTS_STOCK").Value, _
                                               .PARTS_TYPE = Data_Inventory.Element("PARTS_TYPE").Value}).ToList()
Does taking the .Value off the line fix it?
And swap the order of the If: If...Expression...When true...When false
So for your example it would be:
If...Is nothing...Then 0...Else Value

My VB.Net is a tad rusty ;)

Dave
 

antonydenyer

New member
Oct 12, 2008
2
0
0
Use HasElement

Presumably you're using an xelement object, if so check out HasElement

CDbl(If(Data_Inventory.HasElement("PARTS_LENGTH"), Data_Inventory.Element("PARTS_LENGTH").Value, "0")), _

Also I'd recommend putting the logic in it's in it's own method
 

GFR_2009

Member
Oct 9, 2009
37
0
0
Presumably you're using an xelement object, if so check out HasElement

CDbl(If(Data_Inventory.HasElement("PARTS_LENGTH"), Data_Inventory.Element("PARTS_LENGTH").Value, "0")), _

Also I'd recommend putting the logic in it's in it's own method
Thanks for the answer but it doesn't work.

.HasElements is not part of an XElement.

If you have any other idea, please let me know.
 

jonkong

Member
Apr 13, 2007
41
7
0
Hi,

Have you tried:

.PARTS_LENGTH = If(Data_Inventory.Elements("PARTS_LENGTH").Any(), Data_Inventory.Element("PARTS_LENGTH").Value, "0")


Excuse the VB.net it's been a while

Hope it helps.

Kong