简体   繁体   English

将集合添加到IOrderedEnumerable的末尾

[英]Add Collection to End of IOrderedEnumerable

I currently have a bunch of config files I need to load into an IOrderedEnumerable My current approach looks like so: 我目前有一堆配置文件,需要将其加载到IOrderedEnumerable中。我当前的方法如下所示:

foreach (var item in configFiles)
{
    XDocument myxml = XDocument.Load(item);
    var items = myxml.Root.Elements("Item");
    Items = items.OrderBy(x => x.Attribute("ID").Value);
    ItemsLength += Items.Count();              
}

The problem is, instead of making Items equal to items.OrderBy(x => x.Attribute("ID").Value) I'd like to join that to the end of the currently existing IOrderedEnumerable so I'm not overwriting it each time I load a new XDocument and get all of the elements from it. 问题是,不是让Items等于items.OrderBy(x => x.Attribute("ID").Value)我想将其加入到当前现有IOrderedEnumerable的末尾,因此我不会覆盖它每次加载新的XDocument并从中获取所有元素时。 How would I do this? 我该怎么做?

EDIT: I know that if I change this ItemsLength += Items.Count(); 编辑:我知道,如果我更改此ItemsLength += Items.Count(); will no longer function properly. 将不再正常运行。 That's something I'll change on my own. 那是我自己改变的事情。

You can do the whole thing declaratively: 您可以声明性地完成整个操作:

Items = configFiles.Select((item, index) => new { Doc = XDocument.Parse(item),
                                                  Index = index })
                   .SelectMany(pair => pair.Doc.Root.Elements("Item")
                                           .Select(x => new { Item = x, 
                                                              Index = pair.Index }))
                   .OrderBy(pair => pair.Index)
                   .ThenBy(pair => (string) pair.Attribute("ID"))
                   .Select(pair => pair.Item);

This basically finds all the elements, but remembers which configuration each is in. 这基本上找到了所有元素,但记住了每个元素在哪个配置中。

Alternatively, just create a List<XElement> and add each item's contents: 或者,只需创建一个List<XElement>并添加每个项目的内容:

var items = new List<XElement>();
foreach (var item in configFiles)
{
    items.AddRange(XDocument.Parse(item)
                            .Root.Elements("Item")
                            .OrderBy(x => (string) x.Attribute("ID")));
}
Items = items;

In some ways that's less elegant, but it's probably easier to understand :) 在某些方面,它虽然不太优雅,但可能更容易理解:)

If you can change Items to be of type IEnumerable rather than IOrderedEnumerable , you could use Concat : 如果您可以将Items更改为IEnumerable类型而不是IOrderedEnumerable类型,则可以使用Concat

Items = Items.Concat(items.OrderBy(x => x.Attribute("ID").Value));

This would keep items sorted by ID within their own document. 这将使项目在其自己的文档中按ID排序。

Note that using Concat in a loop may impair the performance. 请注意,在循环中使用Concat可能会降低性能。 You would be better off declaring Items as IList , and calling ToList at the end of each iteration: 您最好将Items声明为IList ,并在每次迭代结束时调用ToList

Items.AddRange(items.OrderBy(x => x.Attribute("ID").Value));

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

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