检查“后代”是否属于LINQ查询中存在或!null [英] Check if "Descendants" exists or !null in LINQ query

查看:58
本文介绍了检查“后代”是否属于LINQ查询中存在或!null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用C#和LINQ来解析XML文件。

我需要检查一个元素是否为null因为我将检查后代,如果案例是一个单一元素,这很容易使用扩展方法。



这种检查的原因是有时候该文件不包含docs部分。



这是示例XML:

Hi,

I''m using C# and LINQ to parse a XML file.
I need to check if an element is null or not since I will be checking Descendants, this is easy using an extension method if the case was one singe element.

The reason for this check is that sometimes the file will not contain the "docs" part.

Here is example XML:

<ix:Lang>Eng</ix:Lang>
<ix:docs>
  <ix:libs>qwer</ix:libs>
  <ix:libs>asdf</ix:libs>
</ix:docs>





以下是检查单个元素的方法:



Here is how I check a single element:

//Extension method
public static string ElementValueNull(this XElement element)
        {
            if (element != null)
                return element.Value;

            return "";
        }





这是我用ElementValueNull作为单个元素的一行:



Here is one row where I use "ElementValueNull" for a single element:

Lan = lan.Element(ns + "Lang").ElementValueNull()





但是,我需要使用Descendants将几个孩子循环到docs,libs。

在使用Descendants之前,我怎么能检查元素docs是否为null或存在?



However, I need to use Descendants to loop over several children to "docs", "libs".
How could I check if the element "docs" is null or exists at all before using Descendants on it?

Libs = (from lib in (env.Element(ns + "docs")).Descendants(ns + "libs") select (lib.ElementValueNull())).ToList()





我可以使用其他扩展方法吗?它会是什么样子,放在哪里?



我觉得这可能是微不足道的,我是新来的。

任何帮助将不胜感激,谢谢。



Could I use another extension method? What would it look like and where to place it?

I sense it might be trivial, I''m new at this.
Any help would be appreciated, thanks.

推荐答案

使用 where 子句或哪里扩展方法?





我不确定,但是根据你的评论,你想要做这样的事情,对吗?

Use a where clause or a Where extension method?


Im not sure, but from your comments, you want to do something like this, correct?
XElement node = env.Element(ns + "docs");
if (node != null)
{
   var query = from libs in node.Descendants(ns + "libs")
               where libs != null // Does that make sense? Libs is never null, I guess.
               select libs.Value;
   Libs = query.ToArray();
}
else
{
   // empty list if no docs node available
   Libs = new List<string>();
}



[/ EDIT1]





根据OP的建议,这里还有两个替代方案:


[/EDIT1]


As suggested by OP, here two more alterantives:

public static IEnumerable<XElement> DescendantsOrEmpty(this XElement element, XName name)
{
    return element != null
           ? element.Descendants(name)
           : Enumerable.Empty<XElement>();
}
...
// alternative A): Linq keywords
var query = from lib in env.Element(ns+"docs").DescendantsOrEmpty(ns+"libs")
            select lib.Value ?? string.Empty;
Libs = query.ToList();

// alternative B): Linq extension methods
var query = env.Element(ns+"docs")
            .DescendantsOrEmpty(ns+"libs")
            .Select(lib=>lib.Value ?? string.Empty);
Libs = query.ToList();



[/ EDIT2]



Andi


[/EDIT2]

Andi


我得到了同事的帮助,我们通过另一种扩展方法解决了这个问题:

I got help from a co-worker, we solved this by using another extension method:
public static IEnumerable<xelement> ElementExists(this XElement element, XName param)
{
    if (element != null)
        return element.Descendants(param);

    return Enumerable.Empty<xelement>();
}





这样称呼:



Calling it like this:

Libs = (from lib in (env.Element(ns + "docs")).ElementExists(ns + "libs") select (lib.ElementValueNull())).ToList()


这篇关于检查“后代”是否属于LINQ查询中存在或!null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆