Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to save a LINQ query for later use?

Tags:

linq

I have a request for a feature to be able to save a user's search for later.

Right now I'm building LINQ statements on the fly based on what the user has specified.

So I started wondering, is there an easy way for me to simply take the query that the user built, and persist it somewhere, preferably my database, so that I can retrieve it later?

Is there some way of persisting the query as XML or perhaps JSON, and then reconstituting the query later?

like image 681
Joseph Avatar asked Oct 27 '10 18:10

Joseph


People also ask

Is LINQ or SQL faster?

We can see right away that LINQ is a lot slower than raw SQL, but compiled LINQ is a bit faster. Note that results are in microseconds; real-world queries may take tens or even hundreds of milliseconds, so LINQ overhead will be hardly noticeable.

What does LINQ query return?

By default, LINQ queries return a list of objects as an anonymous type. You can also specify that a query return a list of a specific type by using the Select clause.


2 Answers

Never done this before, but I've had this idea:

Rather than having the query run against your database directly, if you were to have it run against an OData endpoint, you could conceivably extract the URL that is generated as the query string, and save that URL for later use. Since OData has a well-though-out spec already, you would be able to profit from other people's labor.

like image 122
StriplingWarrior Avatar answered Sep 21 '22 12:09

StriplingWarrior


I'd go with a domain-specific object here even if such goodies did exist -- what happens when you save serialized queries in LINQ and your underlying model changes, invalidating everyone's saved queries. Using your own data format should shield you from this to some extent.

like image 39
Wyatt Barnett Avatar answered Sep 18 '22 12:09

Wyatt Barnett