C# How can I tell if an IEnumerable is Mutable?

Posted by Logan on Stack Overflow See other posts from Stack Overflow or by Logan
Published on 2010-05-18T09:41:16Z Indexed on 2010/05/18 9:50 UTC
Read the original article Hit count: 766

Filed under:
|
|

I want a method to update certain entries of an IEnumerable. I found that doing a foreach over the entries and updating the values failed as in the background I was cloning the collection. This was because my IEnumerable was backed by some LINQ->SQL queries.

By changing the method to take a List I have changed this behavior, Lists are always mutable and hence the method changes the actual objects in the list. Rather than demand a List is passed is there a Mutable interface I can use?

    // Will not behave as consistently for all IEnumerables
    public void UpdateYesterday (IEnumerable<Job> jobs) {
          foreach (var job : jobs) {
               if (job.date == Yesterday) {
                   job.done = true;
               }
          }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about mutable