2 Replies Last post: Dec 9, 2008 5:28 AM by JorgeBurgos  
JorgeBurgos Newbie 10 posts since
Feb 3, 2008
Currently Being Moderated

Dec 8, 2008 8:22 AM

Xml to Linq where clause and "Possible System.NullReferenceException"

Often I write a Linq query like the following:

 

from el in _data.Descendants("elements")
where el.Attribute("myattr") != null && el.Attribute("myattr").Value == someValue

select el

 

Resharper always gives a "possible null reference" warning for the second el.Attribute("myattr") reference. I can understand why this warning is given but I can't work out a clean way of rewriting the code to get rid of the warning.

 

Any ideas?

Ilya Ryzhenkov JetBrains 2,694 posts since
Apr 13, 2004
Currently Being Moderated
Dec 8, 2008 10:59 AM in response to: JorgeBurgos
Re: Xml to Linq where clause and "PossibleSystem.NullReferenceException"

Hello JorgeBurgos,

 

Does it help you to rewrite it as follows?

 

from el in _data.Descendants("elements")

let attr = el.Attribute("myattr")

where attr != null && attr.Value == someValue

select el

 

Sincerely,

Ilya Ryzhenkov

 

JetBrains, Inc

http://www.jetbrains.com

"Develop with pleasure!"

 

 

J> Often I write a Linq query like the following:

J>

J> from el in _data.Descendants("elements")
where

J> el.Attribute("myattr") != null && el.Attribute("myattr").Value ==

J> someValue

J>

J> select el

J>

J> Resharper always gives a "possible null reference" warning for the

J> second el.Attribute("myattr") reference. I can understand why this

J> warning is given but I can't work out a clean way of rewriting the

J> code to get rid of the warning.

J>

J> Any ideas?

J>

J> ---

J> Original message URL:

J> http://www.jetbrains.net/devnet/message/5228486#5228486

 

 

 

More Like This

  • Retrieving data ...