LINQ to XML 中的 ISNULL()

发布于 2024-08-07 04:20:31 字数 653 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜扑 2024-08-14 04:20:31

假设您试图避免选取任何没有短名称的内容...

var persons = from person in xmlDoc.Descendants("Table")
    let shortNameElement = person.Element("SHORTNAME") 
    where shortNameElement != null && shortNameElement.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 : "",
    };

或者,您可以使用空合并运算符使所有这些变得更简单:

var emptyElement = new XElement("ignored", "");

var persons = from person in xmlDoc.Descendants("Table")
    where (person.Element("SHORTNAME") ?? emptyElement).Value.Contains("123")
    select new
    {
        shortName = person.Element("SHORTNAME").Value,
        longName = person.Element("LONGNAME").Value,
        address = person.Element("ADDRESS").Value,
        Phone = (person.Element("PHONE") ?? emptyElement).Value
        zip = (person.Element("ZIPCODE") ?? emptyElement).Value
    };

或者,您可以编写一个扩展方法:

public static string ValueOrEmpty(this XElement element)
{
    return element == null ? "" : element.Value;
}

然后像这样使用它:

var persons = from person in xmlDoc.Descendants("Table")
    where person.Element("SHORTNAME").ValueOrEmpty().Contains("123")
    select new
    {
        shortName = person.Element("SHORTNAME").Value,
        longName = person.Element("LONGNAME").Value,
        address = person.Element("ADDRESS").Value,
        Phone = person.Element("PHONE").ValueOrEmpty(),
        zip = person.Element("ZIPCODE").ValueOrEmpty()
    };

Assuming you're trying to avoid picking up anything where there isn't a short name...

var persons = from person in xmlDoc.Descendants("Table")
    let shortNameElement = person.Element("SHORTNAME") 
    where shortNameElement != null && shortNameElement.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 : "",
    };

Alternatively, you can use the null coalescing operator to make all of these a bit simpler:

var emptyElement = new XElement("ignored", "");

var persons = from person in xmlDoc.Descendants("Table")
    where (person.Element("SHORTNAME") ?? emptyElement).Value.Contains("123")
    select new
    {
        shortName = person.Element("SHORTNAME").Value,
        longName = person.Element("LONGNAME").Value,
        address = person.Element("ADDRESS").Value,
        Phone = (person.Element("PHONE") ?? emptyElement).Value
        zip = (person.Element("ZIPCODE") ?? emptyElement).Value
    };

Or alternatively, you could write an extension method:

public static string ValueOrEmpty(this XElement element)
{
    return element == null ? "" : element.Value;
}

and then use it like this:

var persons = from person in xmlDoc.Descendants("Table")
    where person.Element("SHORTNAME").ValueOrEmpty().Contains("123")
    select new
    {
        shortName = person.Element("SHORTNAME").Value,
        longName = person.Element("LONGNAME").Value,
        address = person.Element("ADDRESS").Value,
        Phone = person.Element("PHONE").ValueOrEmpty(),
        zip = person.Element("ZIPCODE").ValueOrEmpty()
    };
烟若柳尘 2024-08-14 04:20:31

使用空合并运算符:

Phone = person.Element("PHONE") ?? String.Empty;

Use the null coalescing operator:

Phone = person.Element("PHONE") ?? String.Empty;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文