ykh
ykh

Reputation: 1825

LINQ ToList() serialization error?

I have a LINQ -> Class file named MoviesDB.dbml, I have added one table "Movie" to it, i have created a class "Movies" in which i have implemented inside all the methods.

Method GetAllMovies which gets a List, when binding the list i get the following:

Type 'DyMvWebsite.Movie' in Assembly 'DyMvWebsite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. 

The code for the Method:

        public static List<Movie> GetAllMovies()
    {
        List<Movie> oMoviesList = new List<Movie>();

        using (MoviesDBDataContext oMoviesDBDataContext = new MoviesDBDataContext())
        {
            oMoviesList = oMoviesList.ToList<Movie>(); ;
        }

        return oMoviesList;
    }

After that i have tried putting the [Serializable] attribute in the "Movies" class and tried putting it in the linq designer file too, but same problem.

Edit:

After following what 'Massimiliano Peluso' suggested, i received a new error:

Type 'System.Data.Linq.ChangeTracker+StandardChangeTracker' in Assembly 'System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable. 

Upvotes: 4

Views: 3334

Answers (2)

Amar Palsapure
Amar Palsapure

Reputation: 9680

Try this out

  MoviesDBDataContext.ObjectTrackingEnabled = false;

before firing the query.

If this still fails, then the work around will be create serializable class similar to Movie class, and map the properties.

Upvotes: 3

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

Everything contained in the Movie class must be Serializable as well

Apply the SerializableAttribute attribute to a type to indicate that instances of this type can be serialized. The common language runtime throws SerializationException if any type in the graph of objects being serialized does not have the SerializableAttribute attribute applied. Apply the SerializableAttribute attribute even if the class also implements the ISerializable interface to control the serialization process. All the public and private fields in a type that are marked by the SerializableAttribute are serialized by default, unless the type implements the ISerializable interface to override the serialization process. The default serialization process excludes fields that are marked with the NonSerializedAttribute attribute. If a field of a serializable type contains a pointer, a handle, or some other data structure that is specific to a particular environment, and cannot be meaningfully reconstituted in a different environment, then you might want to apply the NonSerializedAttribute attribute to that field.

more info:

http://msdn.microsoft.com/en-us/library/system.serializableattribute(v=VS.100).aspx

Upvotes: 2

Related Questions