Here's a challenge for you all

I’ve been toying with the idea of using the core LinqToSql (System.Data.Linq.SqlClient) system to simply generate an sql statement. Without using a data context, or actually executing the query, it should be able to generate the statement and spit it out. An example might look like this:

var sql = SqlQuery<Customer>().Where(x => x.IsActive == true).ToSQL();

And that’s pretty much it. It shouldn’t run the query, or rely on an active connection or command, just build the tsql based on the model.

If you need to write your own IQueryProvider implementation, you may change the SqlQuery to accept it, but I think it should be possible without one.

Let’s see what you can do.

You could not have picked a harder task :). Generation SQL is one of the hardest things to do. I have a book that showed how to do the same thing with WebForms + ADO.NET, and it was waaaaaaay over my head. But, if I get some time I will at least attempt it. If we are going to have everyone in this forum to try, without prizes, thanks SP, lets start out with a smaller task and move up. Maybe I am slow, and some people here can do it. But, I think it will be cool to have challenges every now and then to show off our mad skills, then I’m with ya! :wink:

Glad to see there’s some interest! =)

Ok, I’ll make it a little easier for you guys, and give you a hint as to where to start.

  • Only concern yourself with SqlServer output.

  • Only concern yourself with select statement generation.

Hint:

Given the following code, you should be able to build what you want.

var query = new List<Customer>().Where(x => x.IsActive == true).AsQueryable();

var expression = query.Expression;

Good luck!