views:

121

answers:

2

We're trying to serialize some data, and one of the items in a collection is a "deferred execution linq statement" (actually it's the result of a Concat call on a collection).

The problem is how to persist that object. It doesn't support ISerializable. The actual type is something along the lines of System.Linq.Enumerable.WhereSelectListIterator<>

Just wondering if anyone had run into this before, and what the solution was.

+2  A: 

You can call ToList() on the Linq statement; this will gather all of the results and return them in a List<T> which you can then serialize.

GraemeF
Thanks for the reply. Yeah, that's certainly an option. The problem is that the class in question is templated, and the expression is what's getting provided as a templated argument (which could be anything). So, it doesn't look like we can call ToList() on that. We have to call it on the class that's providing the list, which isn't the most optimum solution. But it's the best we've come up with so far.
DragonDave
+1  A: 

If querying the results and serializing them is not an option, you would have to manually serialize the query somehow.

One problem is that there are too many different LINQ query object types. And all these types are internal to the framework. That pretty much locks you down.

There might a solution for your specific case though. If the only type of query you need to serialize is the result of a Concat call, the solution might not be too complex. All depends on the collections you are concatenating and the way you are (or want to) serialize them.

jpbochi
If the query was built upon IQueryable instead of IEnumerable that would be easier.
Martinho Fernandes
@Martinho: +1 Indeed! serializing lambda expressions is easier then serializing an compiled delegate
jpbochi
Thanks for the reply. Unfortunately, the item being persisted is a templated argument, so it could be anything. The simplest solution we have to far is to go to the class providing that argument, and just call ToList() on it (which we can serialize just fine). We could potentially enumerate the expression and persist each item, but we'd have a problem on the load side determining what kind of object to instantiate.
DragonDave