简体   繁体   English

如何在LINQ to XML中使用条件获取一些数据?

[英]How to get some data using a conditional in LINQ to XML?

I've got a Enumerable and an int array, where I only want to get the data where ObjectiveID exists in the array. 我有一个Enumerable和一个int数组,我只想获取数组中存在ObjectiveID的数据。 For example: 例如:

If this is my array: 1, 2, 3 And in my XML File, I have some nodes where contains: 1, 2, 3, 4, 5, 6 如果这是我的数组:1,2,3在我的XML文件中,我有一些节点,其中包含:1,2,3,4,5,6

The program has to get 1, 2, 3, because exists in the array. 该程序必须获得1、2、3,因为它存在于数组中。 How can I do that? 我怎样才能做到这一点? I was planning using bucles for and remove each one but I know there's a better way to do this. 我当时计划使用气囊来去除每个气囊,但是我知道有一种更好的方法。

This is a part of the code: 这是代码的一部分:

    XDocument xdoc = XDocument.Load(path);
    var conditions = from c in xdoc.Descendants("Condition")
                     select new
                     {
                         ObjectiveID = (int)c.Attribute("ObjectiveID"),
                         TypeID = (int)c.Attribute("TypeID"),
                         ProblemID = (int)c.Attribute("ProblemID"),
                         Ranges = (from r in c.Descendants("Range")
                                   select new
                                   {
                                       Decimals = (int)r.Attribute("Decimals"),
                                       Min = (decimal)r.Attribute("Min"),
                                       Max = (decimal)r.Attribute("Max")
                                   }).ToArray(),
                     };

It's not at all clear where the values 1-6 come, but you probably just want a where clause such as: 值1-6到底从哪里来还不清楚,但是您可能只想要一个where子句,例如:

where array.Contains((int) c.Attribute("id"))

or something similar. 或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM