C# 扩展 where 方法 - 处理未找到元素的情况
我正在尝试使用 xDocument 和 LINQ to XML 查询这个非常复杂的 XML 文档。我想要执行以下操作:
获取满足特定条件的所有元素,如果不满足,则从 xDocument 返回另一个属性。
示例:
<cars>
<car>
<patrol type="oil">
<url> http://Toyotaoil.com </url>
</patrol>
</car>
<car>
<patrol type="oil">
<url> http://BMWoil.com </url>
</patrol>
<patrol type="gas">
<url> http://BMWgas.com </url>
</patrol>
</car>
<car>
<patrol type="gas">
<url> http://Hondagas.com </url>
</patrol>
</car>
现在我想从这个查询中得到的是石油类型的巡逻列表,除非汽车不使用汽油,然后我对汽油感到满意。
如果我使用 where
子句,我就会错过汽车使用汽油的情况。是否有像 where
子句这样的东西,我可以在其中指定如果不满足条件该怎么办?
I am trying to query this very complicated XML document using xDocument and LINQ to XML. I want to perform the following action:
Get all elements that answer to a certain criteria, and if they don't, return another attribute from the xDocument.
Example:
<cars>
<car>
<patrol type="oil">
<url> http://Toyotaoil.com </url>
</patrol>
</car>
<car>
<patrol type="oil">
<url> http://BMWoil.com </url>
</patrol>
<patrol type="gas">
<url> http://BMWgas.com </url>
</patrol>
</car>
<car>
<patrol type="gas">
<url> http://Hondagas.com </url>
</patrol>
</car>
Now what I'd like to get from this query is a list of patrols of type oil, unless the car doesn't use petrol, and then I'd be satisfied with gas.
If I use the where
clause I just miss the cases where the car uses gas. Is there any such thing like a where
clause, where I can specify what to do if they condition wasn't met?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
下面的解决方案应该让您可以灵活地查询您喜欢的任何内容:
我这里没有 Visual Studio,所以我希望它可以编译:)
请提供有关您想要选择的内容的更多信息,我将为您提供更具体的 LINQ 语句。
The solution below should give you the flexibility to query whatever you like:
I don't have Visual Studio here, so I hope it compiles :)
Give a bit more information on what you like to select, and I'll give you a more specific LINQ statement.
你可以做这样的事情:
或者
但是更好地解释问题,谢谢:)
You can just make something like this:
or
But explain the problem better, thanks :)
听起来您实际上根本不需要 where 子句 - 您只需要一个选择一个值或另一个值的 select 子句。
但是,您的示例并没有真正描述您如何根据值选择不同的项目 - 您会选择哪个“其他属性”? “汽车使用汽油的地方”是什么意思?如果您可以提供示例的更多详细信息,那么为您提供匹配的代码应该不会太难。
It sounds like you don't actually want a where clause at all - you just want a select clause that either picks one value or another.
However, your example doesn't really describe how you'd select a different item based on the values - which "other attribute" would you select? What do you mean by "where the car uses gas"? If you can give more details of the example, it shouldn't be too hard to give you matching code.
这些是结果,我得到
alt text http://img694.imageshack.us/ img694/5016/carse.jpg
These are the results, I am getting
alt text http://img694.imageshack.us/img694/5016/carse.jpg
根据您的规格按类型
排序
,采用FirstOrDefault()
。编辑:我的意思是这样的:
这会返回每辆车一个结果。检查
OrderBy
子句并进行相应调整。这将导致:再次编辑:啊,现在它点击了。是的,这只会返回每辆车的一个项目 - Zyphrax 提供了一个很好的解决方案。
Order
by type according to your specifications, takeFirstOrDefault()
.Edit: What I meant was something like this:
This returns one result per car. Check the
OrderBy
clause and adjust it accordingly. This would result in:Edit again: Ah, now it clicked. Yes, this only returns a single item per car - Zyphrax gave a nice solution.