kruegerste
asked on
Handling Let Statement Nulls in LINQ to XML Query
What is the best way to handle null Let statements in LINQ query?
I see one could use the FirstOrDefault() keyword at the end to prevent error on a Let statement as such:
let personsMetaData = persons.Elements("player-m etadata"). FirstOrDef ault()
But then inside the query when assigning values, it throws null reference cause personsMetaData is null.
We are already checking if the Attribute itself is null as such:
Height = (personsMetaData.Attribute ("height") == null) ? null : personsMetaData.Attribute( "height"). Value,
Do we need to also check if the element/variable from the Let statement is also null, as such:
Height = (personsMetaData == null) ? null : ((personsMetaData.Attribut e("height" ) == null) ? null : personsMetaData.Attribute( "height"). Value),
Is there a better way than this? Seems like a lot of code for every attribute or element value. If this is the best option, can anybody show me an extender function that would make this easier to do?
THanks,
Sk
I see one could use the FirstOrDefault() keyword at the end to prevent error on a Let statement as such:
let personsMetaData = persons.Elements("player-m
But then inside the query when assigning values, it throws null reference cause personsMetaData is null.
We are already checking if the Attribute itself is null as such:
Height = (personsMetaData.Attribute
Do we need to also check if the element/variable from the Let statement is also null, as such:
Height = (personsMetaData == null) ? null : ((personsMetaData.Attribut
Is there a better way than this? Seems like a lot of code for every attribute or element value. If this is the best option, can anybody show me an extender function that would make this easier to do?
THanks,
Sk
List<PersonsSportsInfo> personsSportsInfo =
(from persons in Root.Descendants("player")
let personsMetaData = persons.Elements("player-metadata").First()
let firstName = (personsMetaData.Element("name") == null) ? "" : (personsMetaData.Element("name").Attribute("first") == null) ? "" : personsMetaData.Element("name").Attribute("first").Value
let lastName = (personsMetaData.Element("name") == null) ? "" : (personsMetaData.Element("name").Attribute("last") == null) ? "" : personsMetaData.Element("name").Attribute("last").Value
let careerPhase = persons.Descendants("career-phase")
select new PersonsSportsInfo
{
Key = (personsMetaData.Attribute("player-key") == null) ? null : personsMetaData.Attribute("player-key").Value,
UniformNumber = (personsMetaData.Attribute("uniform-number") == null) ? null : personsMetaData.Attribute("uniform-number").Value,
PositionRegular = (personsMetaData.Attribute("position-regular") == null) ? null : personsMetaData.Attribute("position-regular").Value,
Height = (personsMetaData.Attribute("height") == null) ? null : personsMetaData.Attribute("height").Value,
Weight = (personsMetaData.Attribute("weight") == null) ? null : personsMetaData.Attribute("weight").Value,
Status = (personsMetaData.Attribute("status") == null) ? null : personsMetaData.Attribute("status").Value,
DOB = (personsMetaData.Attribute("date-of-birth") == null) ? null : personsMetaData.Attribute("date-of-birth").Value,
Health = (personsMetaData.Attribute("health") == null) ? null : personsMetaData.Attribute("health").Value,
Caliber = (personsMetaData.Attribute(xts + "caliber") == null) ? null : personsMetaData.Attribute(xts + "caliber").Value,
FirstName = firstName,
LastName = lastName,
FullName = firstName + " " + lastName,
PositionDepth = (personsMetaData.Element("player-metadata-american-football") == null) ? null : (personsMetaData.Element("player-metadata-american-football").Attribute(xts + "position-depth") == null) ? "" : personsMetaData.Element("player-metadata-american-football").Attribute(xts + "position-depth").Value,
ProExperience = careerPhase
.Where(c => c.Attribute("phase-type").Value == "professional")
.Select(c => c.Attribute("duration").Value).FirstOrDefault(),
CollegeName = careerPhase
.Where(c => c.Attribute("phase-type").Value == "college")
.Select(c => c.Attribute("name").Value).FirstOrDefault(),
}).ToList();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Not a problem, glad to help.
Thanks; I am feel better. ;=)
Thanks; I am feel better. ;=)
ASKER