473,725 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linq; expression parser?

In Linq, you can apparently get a meaningful body from and
expression's .ToString(); random question - does anybody know if linq
also includes a parser? It just seemed it might be a handy way to
write a safe but easy implementation (i.e. no codedom) for an
IBindingListVie w.Filter (by compiling to a Predicate<T>).

Anybody know if this is possible at all?

Marc

May 25 '07 #1
28 16416
Marc Gravell wrote:
In Linq, you can apparently get a meaningful body from and
expression's .ToString(); random question - does anybody know if linq
also includes a parser? It just seemed it might be a handy way to
write a safe but easy implementation (i.e. no codedom) for an
IBindingListVie w.Filter (by compiling to a Predicate<T>).

Anybody know if this is possible at all?
Why would you use a parser on the string output? Because that parser
will produce a parse tree which will look very similar to the
expression tree you called ToString() on :)

So interpret the expression tree instead.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
May 26 '07 #2
Frans Bouma [C# MVP] <pe************ ******@xs4all.n lwrote:
Marc Gravell wrote:
In Linq, you can apparently get a meaningful body from and
expression's .ToString(); random question - does anybody know if linq
also includes a parser? It just seemed it might be a handy way to
write a safe but easy implementation (i.e. no codedom) for an
IBindingListVie w.Filter (by compiling to a Predicate<T>).

Anybody know if this is possible at all?

Why would you use a parser on the string output? Because that parser
will produce a parse tree which will look very similar to the
expression tree you called ToString() on :)

So interpret the expression tree instead.
I think the point would be to parse expression trees which *hadn't*
been created from ToString, but read in from a file etc. I understood
Marc's introductory sentence to effectively mean: "there's a useful
format for LINQ expressions, as shown by the ToString method".

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 26 '07 #3
Jon has correctly interpreted my witterings... my point being, that
unlike raw .Net 2.0 predicates (etc), LINQ expression trees are quite
structured. Obviously the C# 3-series compiler can parse source to
create an expression, but it is unclear to me (at least until
Reflector supports CLR 3.5 ;-p) how much of this is the compiler and
how much is the runtime.

The ability to parse a string to an expression would be very
powerful... although there would obviously be some limitations in
terms if resolving external entities... but heck: just access to the
expression arguments and literals would be pretty powerful.

Maybe I'm just "off on one"...

Marc

May 26 '07 #4
Jon Skeet [C# MVP] wrote:
Frans Bouma [C# MVP] <pe************ ******@xs4all.n lwrote:
Marc Gravell wrote:
In Linq, you can apparently get a meaningful body from and
expression's .ToString(); random question - does anybody know if
linq also includes a parser? It just seemed it might be a handy
way to write a safe but easy implementation (i.e. no codedom) for
an IBindingListVie w.Filter (by compiling to a Predicate<T>).
>
Anybody know if this is possible at all?
Why would you use a parser on the string output? Because that
parser will produce a parse tree which will look very similar to the
expression tree you called ToString() on :)

So interpret the expression tree instead.

I think the point would be to parse expression trees which hadn't
been created from ToString, but read in from a file etc. I understood
Marc's introductory sentence to effectively mean: "there's a useful
format for LINQ expressions, as shown by the ToString method".
I 'm not sure if I understand you correctly. The C# sourcecode is
parsed to code which builds an expression tree (if I understood
everything correctly) which at runtime results in an Expression tree
with objects. This tree is then passed to the object which is the
source for the query, for example the o/r mapper engine which will
convert the tree to a sql query.

Expression trees aren't serializable, so to store them you need a
representation, which could be a string indeed. However I don't see a
use case for that, as the expression tree only lives at runtime anyway.

Now, to parse the string, you WILL end up with a parse tree (or your
parser isn't that maintainable ;)). This parse tree has likely a lot in
common with the expression tree, so you have to write the tree
traversal code / node interpretation code anyway. So why write the
parser as well, as you already get the expression tree handed to you?

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
May 27 '07 #5
Marc Gravell wrote:
Jon has correctly interpreted my witterings... my point being, that
unlike raw .Net 2.0 predicates (etc), LINQ expression trees are quite
structured. Obviously the C# 3-series compiler can parse source to
create an expression, but it is unclear to me (at least until
Reflector supports CLR 3.5 ;-p) how much of this is the compiler and
how much is the runtime.
it's my understanding that there are two routes:
1) the source the query will work on implements IEnumerable (e.g.
List<T>) This will make the compiler emit calls to extension methods.
2) the source the query will work on implements IQueryable. This will
make the compiler emit code which builds an expression tree. (correct
me if I'm wrong, this is what I understood of it).

So, the code executed at runtime, will build the expression tree for
you and hand it to the source object the query works on (in situation
2). From there, the source object is on its own and can do with the
expression tree what it wants.

This also means that errors in the tree could lead to
runtime-exceptions, not compile-time exceptions. For example the
reference to a method which isn't available according to the
sourceobject.

Now, what it also means is that you don't need a string parser, as
that would lead to a tree similar to the expression tree and you then
have to write the tree intepreter as well, so you win nothing from
using the string parser. There's one exception: expression trees aren't
serializable IIRC, so to serialize them, you could opt for text,
however references to properties/methods are hard to re-build I think.
The ability to parse a string to an expression would be very
powerful... although there would obviously be some limitations in
terms if resolving external entities... but heck: just access to the
expression arguments and literals would be pretty powerful.
Parse a string to an expression is what every parser does ;). You
already have that build into the C# compiler (v3.5), so why re-do that?

FB
Maybe I'm just "off on one"...

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
May 27 '07 #6
Frans Bouma [C# MVP] <pe************ ******@xs4all.n lwrote:

<snip>
The ability to parse a string to an expression would be very
powerful... although there would obviously be some limitations in
terms if resolving external entities... but heck: just access to the
expression arguments and literals would be pretty powerful.

Parse a string to an expression is what every parser does ;). You
already have that build into the C# compiler (v3.5), so why re-do that?
Because you might want to do it at execution time. Suppose the queries
are stored in a configuration file - you don't really want to have to
build valid C# to give it to the C# compiler, if there's a parser which
deals specifically with LINQ queries.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 27 '07 #7
Maybe I'm not explaining myself well...
My point being; the creation of expression trees is not solely the
domain of the compiler (you can create them at runtime through code)
[equally, neither is their execution, hence D-LINQ, object-LINQ, etc).

Occasionally in a system it is necessary to determine some factors at
runtime, for instance through configuration. Rather than write yet
another parser, it would seem highly advantageous if LINQ allowed us a
common supported expression parser. I can think of many uses of such.

It doesn't sound as though anybody is aware of anything... maybe I'll
see what I can cobble together with expression trees. Pity.

Marc

May 27 '07 #8
Jon Skeet [C# MVP] wrote:
Frans Bouma [C# MVP] <pe************ ******@xs4all.n lwrote:

<snip>
The ability to parse a string to an expression would be very
powerful... although there would obviously be some limitations in
terms if resolving external entities... but heck: just access to
the expression arguments and literals would be pretty powerful.
Parse a string to an expression is what every parser does ;). You
already have that build into the C# compiler (v3.5), so why re-do
that?

Because you might want to do it at execution time. Suppose the
queries are stored in a configuration file - you don't really want to
have to build valid C# to give it to the C# compiler, if there's a
parser which deals specifically with LINQ queries.
If the queries are stored in a config file, they're not Linq queries,
IMHO. So if the proposal is: <text in some format-Expression trees,
sure, that can be helpful, but only if the expression tree is then
usefully interpreted by the interpreter you want to use.

The expression tree interpreter and the actual class/object which
executes the interpretation result are very tightly connected. This
means that you can't have an expression tree parser without a class
which does something with the interpretation of the tree. The tree
isn't the goal, it's an intermediate format between Linq query in VB/C#
and a series of commands to execute.

IMHO, it's only useful to store expression trees as text somewhere if
you can't have the C# code somewhere AND the series of commands you
want to execute is produced by a class which only eats expression
trees. Any other situation doesn't justify the route of expression
trees IMHO.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
May 28 '07 #9
Marc Gravell wrote:
Maybe I'm not explaining myself well...
My point being; the creation of expression trees is not solely the
domain of the compiler (you can create them at runtime through code)
[equally, neither is their execution, hence D-LINQ, object-LINQ, etc).

Occasionally in a system it is necessary to determine some factors at
runtime, for instance through configuration. Rather than write yet
another parser, it would seem highly advantageous if LINQ allowed us a
common supported expression parser. I can think of many uses of such.
What's needed is solid documentation how an expression tree looks
like when given Linq elements are expressed in C#/VB. A generic
expression parser is actually not that useful. All it can do is tell
you what kind of node is found at a given position: it doesn't do
anything for you. The tough element is to DO something useful AFTER a
given node or treebranch has been examined and understood.

Interpreting a string similar to interpreting an expression tree is
equal to interpreting the expression tree. At least if you're using an
LL(n) parser. An LR(n) parser shifts/reduces the parse-tree while
evaluating it AND building it so you can best describe the expression
tree as a parse result of an LL(n) parse operation of input text. The
tree is then handed over to the engine which consumes the tree for
doing something with it, be it emitting code, constructing commands etc.

Though, you only need to go that route if you work with the end part
of the sequence just described, i.e. the engine which consumes an
expressiontree. Then you need to feed it an expression tree to produce
something.

If you then need to build queries at runtime, you indeed are out of
luck UNLESS you build the expression tree manually.

That's also why I don't understand why you want to use strings which
are outputted from the expression tree in the first place: you already
have the tree! Not only that, going from strings to expression tree
with a parser effectively re-implements what's already in the C#
compiler. I really fail to see why the effort is needed or even
'powerful' or useful. Please give me a use-case scenario, as things
from configuration is too vague: what are you going to do with the data
from the config file? Why is the strings -expression tree the only
real solution to your situation?

Besides that, generating a piece of C# with the linq queries, compile
it and run it at runtime takes 100 lines of code max.
It doesn't sound as though anybody is aware of anything... maybe I'll
see what I can cobble together with expression trees. Pity.
There are some blank spots here and there, but the overall system
isn't that obscure.

What bugs me is that I still have no clue what you're trying to
achieve.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
May 28 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1715
by: Philipp Brune | last post by:
Hello NG, we are currently beginning to port our Software from VB6 to .NET. At the moment I am looking for an object oriented way to do data-access. LINQ to SQL came in my mind as a very good approach. As Orcas is still available only as a preview we want to write our new code for the .NET Framework 2.0. The problem is, there is no LINQ support so we would have to do another port at the time Orcas is released.
14
21771
by: Ralf Rottmann \(www.24100.net\) | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). Assume I want to select rows from a database and check whether a specific column contains keywords from a list of keywords. The following works just fine: List<stringsearchTerms = new List<string>() { "Maria", "Pedro" };
15
10860
by: EDBrian | last post by:
My problem is this. Our clients create different fields they want to collect and we allow them build dynamic filters, reports etc... We run some TSQL to actually create the column and all works very well. We are now adding a lot more functionality to our filters and could really benefit from using the LINQ to SQL. I have experimented with the Dynamic Linq...
4
4489
by: =?Utf-8?B?Tmljaw==?= | last post by:
Hello, I'm using LINQ to access a SQL Server database. The user needs to be able to duplicate a record. Is there an easy way to do this? I rather not have to set each property from one to the other. I need to copy all the relationships too. Thanks for any help, I really appreciate it. Thanks,
1
1865
by: shapper | last post by:
Hi, I wonder, is there some tool that transforms SQL procedures to LINQ? :-) I want to use LINQ but I have so much work done in SQL that would be great to transform my SQL code to LINQ. Thanks, Miguel
15
6033
by: shapper | last post by:
Hello, I have two Lists: A = {ID, Name} = { (Null, John), (Null, Mary), (Null, Andrew), (Null, Peter) } B = {ID, Name} = { (1, John), (2, Robert), (3, Angela), (4, Andrew) } I want to find which items in A do not exist in B then:
9
2503
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how about vs2008? is it different name space or framework for linq xml or linq sql? ( 2. do we need to have references to what linq's dlls. or namespaces? system core? 3. what name spaces are needed?
0
2363
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's occuring enough to be a real cause for concern. The errors occur a couple times a week and the website is hit with constant traffic 24x7. Below are the two errors that are encountered. I had read something about MARS causing errors like this but...
21
4359
by: hrishy | last post by:
Hi Will LINQ be ported to Python ? regards Hrishy
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.