Is there a way to Deserialize Expressions in C#, I would like to store Expressions in a Database and load them at run time.
-
what kind of expressions? Regex? – Jon Limjap Oct 20 '08 at 10:07
-
16I would assume System.Linq.Expression type expressions, given the "trees". – David Wengier Oct 20 '08 at 10:09
-
1you can use http://www.codeproject.com/Articles/851187/ServicePredicateBuilder-for-creating-Serializable – Mohammad Dayyan Aug 15 '15 at 10:16
7 Answers
I continued work on the library that was mentioned by Serializing and Deserializing Expression Trees in C#
It looks like the project was abandoned (2008) but I did some work on it and now it works with .NET 4.0 and Silverlight. I made bug fixes to their code and also made it more DAL-independent.

- 1
- 1

- 9,605
- 6
- 67
- 94
-
2+1 for coming up with a working solution. I tried using the original library on MSDN and it couldn't deserialize a simple binary expression node. Your project works great. Thanks! – Jacobs Data Solutions Jul 05 '12 at 20:26
-
2
-
@MohammadDayyan, Microsoft has been indecisive and inconsistent on its package management tool. Sometimes ASP.NET CORE packages can be installed with Nuget, in other cases e.g. a MongoDB driver won't be available via NuGet. Still at least Microsoft had the insight to allow NPM to manage packages. More likely to put this on NPM – T. Webster Sep 07 '16 at 03:56
-
1@RepoMan solving a problem is worth so much more to me when it helps someone. Maybe you can contribute some documentation on usage - let's talk.. – T. Webster Sep 07 '16 at 04:06
There's an project on GitHub/Nuget called MetaLinq that aims to make working with expression tress more easy.
It converts between normal Expressions and 'EditableExpressions' which are mutable and fully Serializable, so can be used with Json, Xml, Binary etc.
Also check this blog post for more info.

- 61,751
- 23
- 87
- 115
Other option is the Expression Tree Serialization project on code.msdn.com - would appear to be more what is needed here?
UPD: Now http://archive.msdn.microsoft.com/exprserialization

- 325
- 1
- 3
- 9

- 398
- 2
- 7
-
Expression Tree Serialization code is removed from code gallery http://code.msdn.microsoft.com/exprserialization, any idea where it is moved to or better alternatives? – Kiran Bheemarti Feb 10 '11 at 02:48
-
@Kiran: What do you mean? The code download is still there; check the downloads tab at the top of the page. – Robert Harvey Feb 10 '11 at 02:49
-
Yes it is now, probably a temporary thing, the page was throwing a project not found error yesterday when I tried the link above. I see the project and download now. Thanks!!! – Kiran Bheemarti Feb 10 '11 at 18:02
have a look at my new library Serialize.Linq. It serializes linq expressions to json, xml and binary.

- 10,638
- 3
- 36
- 57
-
have you used this before? how was your experience / find any limitations? – Alexandre Brisebois Sep 24 '12 at 16:37
-
-
Why would you write something that bleeds the lines between tiers worse? You're promoting the exposure of DAL domain objects to the BAL... unless your library somehow automatically converts DTOs (and the queries against them) to domain objects/queries. I don't see anything in the docs indicating such behavior. – M.Babcock Nov 09 '12 at 02:37
-
@M.Babcock: what you want to use in your expressions DTO vs Entities is not part of the logic of the library. – esskar Nov 09 '12 at 11:52
-
4
-
Not in full; however, the Dynamic LINQ sample may help a bit. You can certainly serialize an expression to a string (to an extent - generics look a bit screwy), but there is no inbuilt parser.

- 1,026,079
- 266
- 2,566
- 2,900
-
I can't find out how to serialize an expression to a string using the sample, only the other direction, to take as string and create an expression tree. What method do I call? – Christopher Edwards Apr 14 '11 at 13:56
WCF RIA Services allows for serializing IQuerayble<T>

- 6,705
- 3
- 43
- 54
-
I believe this is more relevant: http://www.silverlightshow.net/items/WCF-RIA-Services-Part-2-Querying-Data.aspx – Enrico Campidoglio Nov 05 '10 at 11:03
I was working on a project for serializing expressions. It's OK and you can use it. http://www.codeproject.com/Articles/851187/ServicePredicateBuilder-for-creating-Serializable

- 21,578
- 41
- 164
- 232