Accessing a range of elements from any point in an IEnumerable

我与影子孤独终老i 提交于 2020-07-08 05:59:38

问题


I have this method:

private IEnumerable<XElement> ReadTransactions(string file_name)
    {
        using (var reader = XmlReader.Create(file_name + ".xml"))
        {
            while (reader.ReadToFollowing("transaction", "urn:namepsaceUri"))
            {
                using (var subtree = reader.ReadSubtree())
                {
                    yield return XElement.Load(subtree);
                }
            }
        }
    }

This method reads from an XML file. However, I don't need all of the nodes in the XML file at same time.

I want to get them ten at a time.

I tried working with XPathSelectElements, but that gets all the nodes, and then I need to iterate through them.

So, is there a way to get the nodes from the XML file which are 40-50? I want to modify ReadTransactions - to have another input parameter (40 in this case), and instead of all the elements, it will return just 10?


回答1:


What about Skip() and Take() extensions methods?

var items = ReadTransactions(file_name).Skip(40).Take(10);



回答2:


what about ElementAt

seems to me this is what you are looking for



来源:https://stackoverflow.com/questions/16295086/accessing-a-range-of-elements-from-any-point-in-an-ienumerable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!