LINQ to XML 中的 ISNULL()
SELECT * FROM CUSTOMERS WHERE RTRIM(ISNULL([SHORTNAME],'')) LIKE '%john%'
我想使用 Linq 编写此内容,
var persons = from person in xmlDoc.Descendants("Table")
where
person.Element("SHORTNAME").Value.Contains("123")
select new
{
shortName = person.Element("SHORTNAME").Value,
longName = person.Element("LONGNAME").Value,
address = person.Element("ADDRESS").Value,
Phone = person.Element("PHONE") != null ? person.Element("PHONE").Value : "",
zip = person.Element("ZIPCODE") != null ? person.Element("ZIPCODE").Value : "",
};
当 [SHORTNAME] 不为空且 [SHORTNAME] 为空值时,此方法可以正常工作这会破坏代码并弹出“空引用异常”
请帮助我......
SELECT * FROM CUSTOMERS WHERE RTRIM(ISNULL([SHORTNAME],'')) LIKE '%john%'
I want to write this using Linq,
var persons = from person in xmlDoc.Descendants("Table")
where
person.Element("SHORTNAME").Value.Contains("123")
select new
{
shortName = person.Element("SHORTNAME").Value,
longName = person.Element("LONGNAME").Value,
address = person.Element("ADDRESS").Value,
Phone = person.Element("PHONE") != null ? person.Element("PHONE").Value : "",
zip = person.Element("ZIPCODE") != null ? person.Element("ZIPCODE").Value : "",
};
This works fine when [SHORTNAME] is not null, if [SHORTNAME] is a null value this breakes the code and pops up a "Null Reference Exception"
Please help me...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您试图避免选取任何没有短名称的内容...
或者,您可以使用空合并运算符使所有这些变得更简单:
或者,您可以编写一个扩展方法:
然后像这样使用它:
Assuming you're trying to avoid picking up anything where there isn't a short name...
Alternatively, you can use the null coalescing operator to make all of these a bit simpler:
Or alternatively, you could write an extension method:
and then use it like this:
使用空合并运算符:
Use the null coalescing operator: