Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to evaluate a string containing valid LINQ dynamically / at runtime?

Tags:

.net

linq

c#-3.0

The basic idea is to take a string and evaluate it against an XML File (or any LINQed provider)

I found this LINQ Dynamic Query Library. Just had a cursory glance at the documentation which is a single page in the download bundle. This seems to be adding extension methods to parameterize parts of the LINQ Query. Does anyone know if this does dynamic evaluation?

Is there any way to just do (assuming there is some way to seed the class with the Data from the XML File) ?

ClassFromWishfulThinking.Evaluate(sLinqQuery);
like image 974
Gishu Avatar asked Jan 27 '09 14:01

Gishu


People also ask

Can you use LINQ on dynamic?

The Dynamic LINQ library let you execute query with dynamic string and provide some utilities methods such as ParseLambda , Parse , and CreateClass .

Can you use LINQ on a string?

LINQ can be used to query and transform strings and collections of strings. It can be especially useful with semi-structured data in text files. LINQ queries can be combined with traditional string functions and regular expressions. For example, you can use the String.

What is Dynamic LINQ?

The Dynamic LINQ library exposes a set of extension methods on IQueryable corresponding to the standard LINQ methods at Queryable, and which accept strings in a special syntax instead of expression trees.


1 Answers

It must be possible because Linqpad does it!

Looking at the how it works page is a bit mind bending, and I'm not sure this is something you would want to do with production code unless you really couldn't avoid it.

There is no framework method that just does what you want, I can say that with a fairly high degree of confidence because of how .net works. You have to call the compiler using the csharpcodeprovider as LinqPad does.

You might be able to change the Linq Dynamic Query Library to do what you need.

like image 87
Christopher Edwards Avatar answered Sep 25 '22 16:09

Christopher Edwards