How to make your own Linq articles?

Is there a way to create my own Linq articles? I studied extension methods, and this is not quite what I am looking for.

I was thinking of something like a sentence, select or from. I want to use my code like

var blah = from bleep in bloops where bleep == razzie myclause bleep.property select bleep; 
+4
source share
5 answers

You cannot change the interpretation of compiler requests. They are referred to as understanding of queries - and the rules for translating them are built directly in the C # compiler. Each request is converted into an appropriate sequence of calls into extension methods in the Linq library.

If you use tools such as Reflector, you will see the corresponding sequence of calls to the extension method into which such expressions will be translated.

The rules for understanding queries are fully described in the C # 3.0 specification .

Although I agree that there are some specific cases where it may be useful to extend the syntax of a query in a language, I suspect that it requires significant complex compilation time processing to convert it to the corresponding function invocation syntax. I don’t think that in most cases it would be easy to just introduce processing for special cases without affecting the conversion of the whole expression.

At the same time, be aware that you can use chaining and regular extension methods to extend Linq's capabilities.

+4
source

To do this, you need access to the source code of the C # compiler. I would stick with extension methods. If you have a copy of Reflector , you can take a look at System.Data.Linq and see how the original extension methods were written.

+2
source

I do not understand why you could not use extension methods. Using the above query, you should convert it from what you have something like this:

 var blah = (from bleep in bloops where bleep == razzie select bleep).myclause(bleep.property); 
0
source

This syntax is just sugar around ...

 var blah = bloops.Where( b => b == razzie ); 

And it is not extensible ..

You can, of course, add your own sentences (just extension methods), you just won't have syntactic sugar.

0
source

This is not possible in C #, but if you really want to add the syntax yourself, you can write a preliminary C # compiler that includes additional keywords to understand the request. To implement it in the same way as C # does (as in the specification ), you would find, for example, a valid extension method MyClause() for the return type of the source object or the previous return type in the call chain, for example, IEnumerable<T> .

Your example:

 var blah = from bleep in bloops where bleep == razzie myclause bleep.property select bleep; 

will become:

 var blah = bloops.Where(bleep => bleep==razzie) .MyClause(bleep => bleep.property) .Select(bleep => bleep); 
0
source

All Articles