Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用LINQ where子句忽略特定的?_C#_Linq - Fatal编程技术网

C# 如何使用LINQ where子句忽略特定的?

C# 如何使用LINQ where子句忽略特定的?,c#,linq,C#,Linq,我有一个客户端,它发送一个xml提要,我使用以下代码对其进行解析。这个代码有效 reviews = from item in xmlDoc.Descendants("node") select new ForewordReview() { PubDate = (string)item.Element("created"),

我有一个客户端,它发送一个xml提要,我使用以下代码对其进行解析。这个代码有效

reviews = from item in xmlDoc.Descendants("node")
                          select new ForewordReview()
                          {
                              PubDate = (string)item.Element("created"),
                              Isbn = (string)item.Element("isbn"),
                              Summary = (string)item.Element("review")
                          };
在得到我所有的“评论”后,我将IEnumerable转换为一个列表并返回。最初,我很容易解析他们的XML,以前的XML是这样的:

<reviews>
    <node>
        <created>01-01-1900</created>
        <ISBN>12345657890123</ISBN>
        <Review>This is a nice and silly book</Review>
    </node>
    <node>
        <created>01-01-2011</created>
        <ISBN>1236245234554</ISBN>
        <Review>This is a stupid book</Review>
    </node>
    <node>
        <created>12-06-1942</created>
        <ISBN>1234543234577</ISBN>
        <Review>This is a old, naughty book</Review>
    </node>
</reviews>
var count = xmlDoc.Descendants("node").Count();
xmlDoc.Descendants("node").Where((node,index) => index < count-1).Select(..)

01-01-1900
12345657890123
这是一本又好又傻的书
01-01-2011
1236245234554
这是一本愚蠢的书
12-06-1942
1234543234577
这是一本淘气的旧书
然而,他们已经更改了他们的模式,我没有访问权限,现在他们的XML在末尾添加了一个最后的
标记,该标记不包含我正在查找的decedent元素,因此,我对最后一个标记的解析中断,我抛出了一个异常。以下是一个例子:

<reviews>
    <node>
        <created>01-01-1900</created>
        <ISBN>12345657890123</ISBN>
        <Review>This is a nice and silly book</Review>
    </node>
    <node>
        <created>01-01-2011</created>
        <ISBN>1236245234554</ISBN>
        <Review>This is a stupid book</Review>
    </node>
    <node>
        <created>12-06-1942</created>
        <ISBN>1234543234577</ISBN>
        <Review>This is a old, naughty book</Review>
    </node>
    <node>
        <count>4656</count>
    </node>
</reviews>

01-01-1900
12345657890123
这是一本又好又傻的书
01-01-2011
1236245234554
这是一本愚蠢的书
12-06-1942
1234543234577
这是一本淘气的旧书
4656
我需要知道是否有办法忽略最后一个标记(它总是在文档的末尾),即使它与我正在寻找的所有其他“节点”标记具有相同的名称。我确实有一个关于这段代码的尝试,但是如果它看到这个错误,它不会返回好的评论列表


谢谢大家。

如果它总是最后一个节点

var nodes = xmlDoc.Descendants("node");
reviews = nodes.Take(nodes.Count() - 1).Select(...);
添加空检查

PubDate = (string)(item.Element("created") ?? ""),
Isbn = (string)(item.Element("isbn") ?? ""),
Summary = (string)(item.Element("review") ?? "")

始终将空检查添加到您所做的每件事中。只是良好的实践。在这种情况下,它将消除此错误,但在以后的程序中可能会出现一个错误,您假定这些字符串不是空的,因此请确保稍后也进行null检查。

您可以计算节点数,然后使用where重载,该重载还传递一个索引号:(http://msdn.microsoft.com/en-us/library/bb549418.aspx)

公共静态IEnumerable其中(
这是一个数不清的来源,
Func谓词)
比如说:

<reviews>
    <node>
        <created>01-01-1900</created>
        <ISBN>12345657890123</ISBN>
        <Review>This is a nice and silly book</Review>
    </node>
    <node>
        <created>01-01-2011</created>
        <ISBN>1236245234554</ISBN>
        <Review>This is a stupid book</Review>
    </node>
    <node>
        <created>12-06-1942</created>
        <ISBN>1234543234577</ISBN>
        <Review>This is a old, naughty book</Review>
    </node>
</reviews>
var count = xmlDoc.Descendants("node").Count();
xmlDoc.Descendants("node").Where((node,index) => index < count-1).Select(..)
var count=xmlDoc.substands(“节点”).count();
xmlDoc.substands(“节点”)。其中((节点,索引)=>index
像这样的东西应该可以做到:

var reviews = from item in xmlDoc.Descendants("node").Where(x => x.Element("created") != null)
select new
{
    PubDate = (string)item.Element("created"),
    Isbn = (string)item.Element("isbn"),
    Summary = (string)item.Element("review")
};
如果愿意,您可以对其他元素进行额外的空检查。

您可以在其中抛出一个“where”子句

reviews = from item in xmlDoc.Descendants("node") where item.Descendants().Any(n => n.Name == "Review")
                      select new ForewordReview()
                      {
                          PubDate = (string)item.Element("created"),
                          Isbn = (string)item.Element("isbn"),
                          Summary = (string)item.Element("review")
                      };

此示例只检查名为“Review”的子节点,因此最好检查所有必需的子节点。

它始终是最后一个“节点”,但不是最后一个元素。“reviews”元素是根元素。这有关系吗?@fullNelson:这将取所有名为“node”的元素除了最后一个。你读过代码吗?我读过。我从来没有使用过这种语法风格(因为我是linq新手),所以我不知道它是如何工作的。@fullNelson:它们完全是普通的扩展方法调用。你肯定以前用过方法吗?也许可以添加一个
where item.Element(“count”)==null
(或者是“created”!=null)@ordag我实际上也将您的合并到了我的解决方案中,以及delltrees建议的一些空检查。谢谢。因此,如果它遇到了奇怪的节点,它将在一个对象中加载pubdate、isbn和summary,并给每个对象一个空字符串?如果是这样的话,我肯定可以处理下游。确切地说。string。empty可能是更好的选择r的选择,但这只是一个快速的选择writeup@deltree您的解决方案实际上是一个非常好的主意,并公开了我以前没有的空检查。我也尝试了where子句,但您得到了批准。我确信我知道这里发生了什么,但代码中语法的差异让我感到困惑。我想您和Matti正在使用的是一个“点”符号。对我来说,我不知道如何在where语句中的子句之前添加此筛选器。@fullnelson同样的语句使用linq语法更容易编写/读取,有些语句使用扩展方法更容易编写/读取(如上所述)。很高兴知道linq语句可以直接转换为扩展方法。而这种转换正是编译器在编写“linq语法”时所做的,这使得整个语法有点多余。我并不真的同意你的观点。在我看来,使用“linq语法”时,有些语句更可读。因此,这并不意味着它是多余的。