Skip to content

DataContractSerializer no longer supports IEnumerable instances backed by LINQ OrderBy #34151

@Treit

Description

@Treit

Similar to this issue with empty enumerables, an IEnumerable that is actually backed by a LINQ OrderBy expression can no longer be serialized by DataContractSerializer in .NET Core.

This change in behavior breaks a lot of existing code at runtime.

The following code sample illustrates the problem. It works fine .NET Framework but fails on .NET Core 3.1.

namespace Test
{
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    
    [DataContract]
    public class TestClass
    {
        [DataMember]
        public IEnumerable<string> Stuff { get; set; }
    }
    class Program
    {
        static void Main()
        {
            TestClass t = new TestClass
            {
                Stuff = new List<string> { "a", "b", "c" }.OrderBy(x => x)
            };
            
            DataContractSerializer dcs = new DataContractSerializer(typeof(TestClass));
            using MemoryStream stream = new MemoryStream();
            dcs.WriteObject(stream, t);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions