tags:

views:

112

answers:

2

I ran into an error when trying to serialize a linq entitySet. To get around this i converted the entitySet to a list. The problem I have run into now is that it's child entity sets are not converting to a list and when I try to serialize the parent those are now throwing an error. Does anyone know of a way to convert a linq entitySet AND it's children to lists?

p.s. I'm new to linq so if any of this dosn't make sense let me know

A: 

I am guessing you are trying to serialize to XML.

Either way, the problem stems from the circular references in LINQ entity objects. Lets say you have a main table Customers with a second table Orders. The Customers entity has a reference to all of the Orders this customer has (typically Customer.Orders). The Orders entity has a reference to the customer entity (typically Order.Customer). When the serializer tries to serialize this object, it recognizes the circular reference and throws an error.

In order to do what you want, you have to write your own serializer or convert the objects to something that can be directly serialized, like custom serialization objects which contain just the information you want to show up in the serialized version.

NickLarsen
A: 

Just project onto new types:

var q = from e in Entities
        select new 
        {
            Id = e.Id,
            Name = e.Name,
            Children = from c in e.Children
                       select new 
                       {
                           Id = c.Id,
                           Name = c.Name,
                           // etc.
                       },
             // etc.
         };
var s = serializer.Serialize(q);
Craig Stuntz