简体   繁体   中英

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. 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

The program has to get 1, 2, 3, because exists in the array. 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:

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

or something similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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