开发者

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开发者_StackOverflow中文版, 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜