Sql – How to intercept and modify SQL query in Linq to SQL


I was wondering if there is any way to intercept and modify the sql generated from linq to Sql before the query is sent off?

Basically, we have a record security layer, that given a query like 'select * from records' it will modify the query to be something like 'select * from records WHERE [somesecurityfilter]'

I am trying to find the best way to intercept and modify the sql before its executed by the linq to sql provider.

Best Solution

If you want to intercept the SQL generated by L2S and fiddle with that, your best option is to create a wrapper classes for SqlConnection, SqlCommand, DbProviderFactory etc. Give a wrapped instance of SqlConnection to the L2S datacontext constructor overload that takes a db connection. In the wrapped connection you can replace the DbProviderFactory with your own custom DbProviderFactory-derived class that returns wrapped versions of SqlCommand etc.


//sample wrapped SqlConnection:
public class MySqlConnectionWrapper : SqlConnection
  private SqlConnecction _sqlConn = null;
  public MySqlConnectionWrapper(string connectString)
    _sqlConn = new SqlConnection(connectString);

  public override void Open()

  //TODO: override everything else and pass on to _sqlConn...

  protected override DbProviderFactory DbProviderFactory
    //todo: return wrapped provider factory...

When using:

using (SomeDataContext dc = new SomeDataContext(new MySqlConnectionWrapper("connect strng"))
  var q = from x in dc.SomeTable select x;

That said, do you really want to go down that road? You'll need to be able to parse the SQL statements and queries generated by L2S in order to modify them properly. If you can instead modify the linq queries to append whatever you want to add to them, that is probably a better alternative.

Remember that Linq queries are composable, so you can add 'extras' in a separate method if you have something that you want to add to many queries.