简体   繁体   English

为什么我不能编辑IEnumerable的元素?

[英]Why can't I edit elements of IEnumerable?

I do something like this and the value in the collection doesn't change 我做这样的事情,集合中的价值不会改变

                [Test]
                public void EnumerableTest()
                {
                    var source = GetFoos();

                    source.First().One = "hello";

                    Assert.AreEqual(source.First().One, "hello");
    //it fails

                }

//I actually return this from a repository
                public IEnumerable<Foo> GetFoos()
                {
                    yield return new Foo() {One = "1", Two = "2", Three = true};
                    yield return new Foo() {One = "1", Two = "2", Three = true};
                    yield return new Foo() {One = "1", Two = "2", Three = true};
                }

这是因为每次枚举GetFoos时都会创建新实例。

If you change the var source = GetFoos(); 如果你改变了var source = GetFoos(); into var source = GetFoos().ToList(); into var source = GetFoos().ToList(); , the list is read immediately (and in full). ,列表立即(和完整)读取。 Then you should be able to change the values. 然后你应该能够改变价值观。

Don't forget to store the changed values or else they revert the next time you read them. 不要忘记存储更改的值,否则它们会在您下次阅读时还原。

It is because of your use of yield return . 这是因为你使用了yield return

You could write instead: 你可以写:

public IEnumerable<Foo> GetFoos()
{
    return new List<Foo>
    {
        new Foo { One = "1", Two = "2", Three = true },
        new Foo { One = "1", Two = "2", Three = true },
        new Foo { One = "1", Two = "2", Three = true },
    };
}

When you call First() a new Enumerator is created. 当您调用First()会创建一个新的枚举器。 So GetFoos() is called again and return a new object. 因此再次调用GetFoos()并返回一个新对象。

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

相关问题 为什么我不能在for循环中更改linq IEnumerable中的元素? - Why can't I change elements from a linq IEnumerable in a for loop? 为什么我不能返回IEnumerable <IGrouping<A,B> &gt;作为IEnumerable <IEnumerable<B> &gt; - Why can't I return an IEnumerable<IGrouping<A,B>> as an IEnumerable<IEnumerable<B>> 如何检测IEnumerable中的“缺失”元素 <T> ? - How can I detect “missing” elements in an IEnumerable<T>? 为什么我不能将IEnumerable <T>列表转换为BindingList <t>? - Why can i not cast an IEnumerable<T> list to a BindingList<t>? 如果我只能定义一个GetEnumerator,为什么还要实现IEnumerable(T)? - Why implement IEnumerable(T) if I can just define ONE GetEnumerator? 为什么我不能通过 IList<childtype> 到 F(IEnumerable<parenttype> )?</parenttype></childtype> - Why can't I pass IList<ChildType> to F(IEnumerable<ParentType>)? 为什么我不能使用以下IEnumerable <string> ? - Why can't I use the following IEnumerable<string>? 无法隐式转换为IEnumerable <T> ……但是我可以反过来……为什么? - Cannot implicitly cast to IEnumerable<T>… but I can the other way… why? 试图了解我的 IEnumerable 以及为什么我不能使用.ToList() - Trying to understand my IEnumerable and why I can't use .ToList() 为什么我不能将List <List <Foo >>传递给IEnumerable <IEnumerable <Foo >> - Why can't I pass a List<List<Foo>> to an IEnumerable<IEnumerable<Foo>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM