Linq To Sql objects and Serialization issue

I am trying to serialize an object graph. It works up until I have to load a child entity that is an entity set. The system won't allow it to serialize... it gives me this error:

Type 'System.Data.Linq.EntitySet`1[NetDataContractSerializerTest.JobService]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

Here is the code I am using:

        DataClasses1DataContext dataAccessContext = new DataClasses1DataContext();
        MemoryStream stream = new MemoryStream();

        DataLoadOptions dataloadOptions = new DataLoadOptions();
        dataloadOptions.LoadWith<JN>(j => j.nh);  
        dataloadOptions.LoadWith<JN>(j => j.JobServices);// this is the child entity set

        dataAccessContext.LoadOptions = dataloadOptions;

        var jn= dataAccessContext.JN.Where(j => j.LocnID.Trim() == "HT").ToList();
        var netDataContractSerializer = new NetDataContractSerializer();
        netDataContractSerializer.Serialize(stream, jn); //the error happens here

If I remove the

dataloadOptions.LoadWith(j => j.JobServices)

the data will serialize with the JN and nh tables intact. But when I put

dataloadOptions.LoadWith(j => j.JobServices)

back in I get the error.

I personally think that it wants me to add a ToList() to the JobServices, unfortunately I can't do

dataloadOptions.LoadWith(j => j.JobServices.ToList())

This throws an error.

Any ideas?


The error message is very informative: the JobService class is not marked to be serialized as a data contract.

You'll need to decorate it with the DataContractAttribute attribute, and decorate the members with DataMember attribute.

public class JobService
     public string SomeProperty { get; set;}


Well well well.... look what I found..... This seemed to serialize and deserialize my stuff just fine. Instead of using NetDataContractSerializer.Serialize() to it, this guy wrote serialize and deserialize functions that used DataContractSerializer, XMLWriter and XMLReader

Check the link below

Need Your Help

What's the difference between Webcal and WebDAV/CalDAV?

icalendar webdav caldav webcal

As I understand it, CalDAV is an extension to WebDAV to manage iCalendar subscriptions.

cocos2dx android crash upon Facebook Login

android facebook android-ndk cocos2d-x facebook-android-sdk

I am integrating Facebook SDK for Android on my cocos2d-x-2.2 app

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.