Strangest LINQ to SQL case I have ever seen

Posted by kubaw on Stack Overflow See other posts from Stack Overflow or by kubaw
Published on 2010-05-28T10:05:35Z Indexed on 2010/05/28 10:22 UTC
Read the original article Hit count: 140

Filed under:
|
|

OK, so this is the strangest issue in .net programming I have ever seen. It seems that object fields are serialized in .net web services in order of field initialization.

It all started with Flex not accepting SOAP response from .net web service. I have found out that it was due to the order of serialized fields was statisfying the order of fields in declared serializable class.

It had something to do with generic lists and LINQ to SQL but I can't find out what. This one is really hard to reproduce.

Example to get the idea:

[Serializable] 
public class SomeSample 
{ 
    public int A; 
    public int B; 
    public int C; 
} 

I was querying some data tables within asmx web service using linq and returning list of SomeSample objects:

var r = (from ...... select new SomeSample { A = 1, C = 3 }).ToList(); 

Now the list was once more iterated and B field was applied some value (ex. 2).

However the returned soap envelope contained following excerpt:

<A>1</A><C>3</C><B>2</B> 

Please notice the order of serialization. If I initially initialized all fields:

var r = (from ...... select new SomeSample { A = 1, B = 2, C = 3 }).ToList(); 

object was serialized in correct order.

I must add, that in both cases the debugger shows exactly the same content of "r" variable.

Am I losing my mind or is this normal behavior?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about sql

Related posts about LINQ