Use text representations of LINQ expressions
In my project, i use a lot of LINQ expressions and i would allow users to type their own queries by typing a linq query in a text input
So, is it possible to create a LINQ expression from a text containing this one ? And, moreover, is it possible to get the tex开发者_如何学Got equivalent of a linq query ? (like from FOO in bar select foo)
Thank's by advance
You can try NLinq: http://nlinq.codeplex.com
If you were to take on this task, you would essentially be doing the job of the compiler (taking text representation of an expression and creating an expression tree out of it. This is a lot of work and probably not worth your time. The first thought that comes to mind is to have other's type in the linq query and then you compile it on the fly. The obvious downside to this is that once an assembly is loaded in memory, you can't unload it (unless you start using AppDomains), so this would basically turn into a memory leak.
Since you're using Entity Framework, the best answer is probably to use ESQL, http://innocraft.spaces.live.com/blog/cns!919A8CAC315ADF82!239.entry. This is always worked with in text format and Entity Framework will parse it out for you and run the query at runtime.
HTH
Is it possible you could give your users a copy of LinqPad (which is free), and they can create their own queries, and also see the SQL text equivalent?
LinqPad can be found here
You could also consider Dynamic LINQ. It's still bundled with VS2010 (somwehere in the Samples folder) although it's not very well documented. There's a brief intro here:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
精彩评论