8

Is there anyway to serialize LINQ query in order to persist it and use it later?

Something like this:

var serializedQuery = context.Users.Where(row => row.Id == 1).Serialize();

It can be serialized to XML, JSON or Binary as long as I can parse it back to a LINQ query.

kdelmonte
  • 680
  • 1
  • 10
  • 16
  • Serialize it to what? JSON? XML? Binary? – Der Kommissar Apr 27 '15 at 20:50
  • You mean the query itself, not the results, right? – Blorgbeard Apr 27 '15 at 20:50
  • Yes, the query itself. It can be serialized to XML, JSON or Binary as long as I can parse it back to a LINQ query. – kdelmonte Apr 27 '15 at 20:51
  • is your question different from http://stackoverflow.com/questions/11404757/save-entity-framework-linq-query-to-database – Praveen Paulose Apr 27 '15 at 20:53
  • 3
    [first result on Google...](https://github.com/esskar/Serialize.Linq) – Codeman Apr 27 '15 at 20:54
  • Seems to be the same. So you need a third party library to do this... I would've thought it was an out of the box feature. – kdelmonte Apr 27 '15 at 20:55
  • I would think most people would just implement a compiler and fully compile C# code (like linqpad) over doing something like this. Seems more functional and there are a number of examples. – Hogan Apr 27 '15 at 20:59
  • 2
    Well, what is the actual problem you're solving that requires serialized linq queries? – Blorgbeard Apr 27 '15 at 21:01
  • possible duplicate of [C# How to serialize system.linq.expressions?](http://stackoverflow.com/questions/18143181/c-sharp-how-to-serialize-system-linq-expressions) – Codeman Apr 27 '15 at 21:02

1 Answers1

-2

Sure, you can use the first result on google which is an open source library that shows you exactly how to do it. Or you can just use the library as-is.

There's no out of the box feature to do it, as far as I know. Serializing method signatures and IQueryables is a sticky problem.

Codeman
  • 12,157
  • 10
  • 53
  • 91
  • 3
    this isn't really an answer, it would be better as a comment. Check the SO Help Center for tips on better answers: https://stackoverflow.com/help/how-to-answer – jth41 May 26 '17 at 14:22
  • @jth41 not really, and I did comment above. No other answers were made so I made this one. – Codeman May 30 '17 at 18:52