Wiki Link: [discussion:2483]
Predicate with Comparison to Null will not Serialize 

Nov 2 2009 at 9:14 PM
I am unable to serialize expressions that contain a comparison to 'null' as in the expression
Expression<Func<Order, bool>> predicate = DynamicExpression.ParseLambda<Order, bool>("Customer != null");

The predicate evaluates just fine (as in query = query.Where(predicate)) but serialization fails.

Expression Tree Serialization is trying to serialize the null value itself in this function:

private object GenerateXmlFromObject(string propName, object value)
{
object result = null;
if (value is Type)
result = GenerateXmlFromTypeCore((Type)value);
if (result == null)
result = value.ToString(); // <--- fails here because value is null
return new XElement(propName,
result);
}

Any suggestions on how to fix/work around this? Is there a way to state my predicate that avoids serializing the null value?

kb

Here's the stack trace:
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="ExpressionSerialization"
StackTrace:
at System.Linq.Expressions.Serialization.ExpressionSerializer.GenerateXmlFromObject(String propName, Object value) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 108
at System.Linq.Expressions.Serialization.ExpressionSerializer.GenerateXmlFromProperty(Type propType, String propName, Object value) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 78
at System.Linq.Expressions.Serialization.ExpressionSerializer.<>c_DisplayClass1.<GenerateXmlFromExpressionCore>b_0(PropertyInfo prop) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 52
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XElement..ctor(XName name, Object content)
at System.Linq.Expressions.Serialization.ExpressionSerializer.GenerateXmlFromExpressionCore(Expression e) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 49
at System.Linq.Expressions.Serialization.ExpressionSerializer.GenerateXmlFromExpression(String propName, Expression e) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 214
at System.Linq.Expressions.Serialization.ExpressionSerializer.GenerateXmlFromProperty(Type propType, String propName, Object value) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 80
at System.Linq.Expressions.Serialization.ExpressionSerializer.<>c_DisplayClass1.<GenerateXmlFromExpressionCore>b_0(PropertyInfo prop) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 52
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XElement..ctor(XName name, Object content)
at System.Linq.Expressions.Serialization.ExpressionSerializer.GenerateXmlFromExpressionCore(Expression e) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 49
at System.Linq.Expressions.Serialization.ExpressionSerializer.GenerateXmlFromExpression(String propName, Expression e) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 214
at System.Linq.Expressions.Serialization.ExpressionSerializer.GenerateXmlFromProperty(Type propType, String propName, Object value) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 80
at System.Linq.Expressions.Serialization.ExpressionSerializer.<>c_DisplayClass1.<GenerateXmlFromExpressionCore>b_0(PropertyInfo prop) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 52
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XElement..ctor(XName name, Object content)
at System.Linq.Expressions.Serialization.ExpressionSerializer.GenerateXmlFromExpressionCore(Expression e) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 49
at System.Linq.Expressions.Serialization.ExpressionSerializer.GenerateXmlFromExpression(String propName, Expression e) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 214
at System.Linq.Expressions.Serialization.ExpressionSerializer.GenerateXmlFromProperty(Type propType, String propName, Object value) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 80
at System.Linq.Expressions.Serialization.ExpressionSerializer.<>c_DisplayClass1.<GenerateXmlFromExpressionCore>b_0(PropertyInfo prop) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 52
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XElement..ctor(XName name, Object content)
at System.Linq.Expressions.Serialization.ExpressionSerializer.GenerateXmlFromExpressionCore(Expression e) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 49
at System.Linq.Expressions.Serialization.ExpressionSerializer.Serialize(Expression e) in ExpressionTreeSerialization\ExpressionTreeSerialization\ExpressionSerialization\ExpressionSerializer.cs:line 39
at ContractMgmt.DataServer.Filter.ExpressionList`1.GetObjectData(SerializationInfo info, StreamingContext context) in C:\Desk\Code\PlanetWare\IIF Data\ContractMgmt\DataServer\Filter\ExpressionList.cs:line 45
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
at System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer, Object value)


Updating...
Page view tracker