views:

204

answers:

5

Is there a way I can create my own Linq Clauses? I have been looking into extension methods and it's not quite what I am looking for.

I was thinking something like the where, the select or the from clause. I want to use my code like such

var blah = from bleep in bloops
           where bleep == razzie
           myclause bleep.property
           select bleep;
+2  A: 

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

Robert Harvey
A: 

I don't see why you couldn't use extension methods. Using the above query you would convert it from what you have to something like this:

var blah = (from bleep in bloops
           where bleep == razzie
           select bleep).myclause(bleep.property);
Andrew Siemer
what if i wanted to make a special type of join? or try to mimic different functionality that sql offers that might be useful against IQueryable?
Russ Bradberry
@Russ: You can do all that with extension methods, it just won't translate into query syntax. For example: `var blah = bloops.YourSpecialJoin(blobs).Where(bleep => bleep == razzie);`
LukeH
A: 

That syntax is just some sugar around...

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

And that is not extendable..

You can of course add your own clauses (just extension methods) you just wont have the syntacical sugar.

Tim Jarvis
+4  A: 

You cannot change the way the compiler interprets query statements. These are referred to as query comprehensions - and the rules for translating these are built directly into the C# compiler. Every query is translated into an appropriate sequence of calls to into the extensions methods within the Linq library.

If you use tools like Reflector, you will see the corresponding sequence of extension method calls that such expressions are translated into.

The rules for query comprehensions are fully documented in the C# 3.0 specification.

While I agree that there are some specific cases where it might be useful to extend the query syntax in the language, I suspect that it requires a significant amount of sophisticated compile-time processing to convert them into appropriate function call syntax. I don't think that in most cases it would be easy to just inject processing for special cases without affecting how the entire expression is transformed.

In the meantime, realize that you can use chaining and regular extensions methods to expand on the capabilities of Linq.

LBushkin
A: 

This isn't possible within C#, but if you really want to add the syntax yourself, you can write a C# pre-compiler that includes the extra query comprehension keywords. To implement it the same way that C# does (as in the specification), you'd search for a, e.g., valid MyClause() extension method, for the return type of the source object or the previous return type in the call chain, e.g., IEnumerable<T>.

Your example:

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

would become:

var blah = bloops.Where(bleep => bleep==razzie)
                 .MyClause(bleep => bleep.property)
                 .Select(bleep => bleep);
Mark Cidade