Search code examples
c#serializationsession-statelinq-expressions

Serialize a class with Linq expression properties for SQL Server Session State


I have a class which has some expression tree properties. when I try to serialize it using the serializable attribute in order to configure SQL Server Session State, I have got the following error: ..Unable to serialize the session state, SerializationException: Type 'System.Linq.Expressions.Expression..., as the picture shows.

Does anybody know how can I solve that problem to be able to manage session state on SQLServer mode. Thanks.

enter image description here

My class looks to something like this:

[Serializable]
public class Elements<T>
{
    public List<T> elementsList { get; set;}
    Expression<Func<int, bool>> lambda = num => num < 5;

}

Solution

  • It would make no sense to serialize an Expression. You should ignore it.

    [Serializable]
    public class Elements<T>
    {
        public List<T> elementsList { get; set;}
    
        [NonSerialized]
        Expression<Func<int, bool>> lambda = num => num < 5;
    }
    

    Look here for more info: https://msdn.microsoft.com/en-us/library/system.nonserializedattribute(v=vs.110).aspx