Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3444494
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:55:21+00:00 2026-05-18T08:55:21+00:00

I was wondering if there is any way to intercept and modify the sql

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:55:21+00:00Added an answer on May 18, 2026 at 8:55 am

    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.

    E.g.:

    //sample wrapped SqlConnection:
    public class MySqlConnectionWrapper : SqlConnection
    {
      private SqlConnecction _sqlConn = null;
      public MySqlConnectionWrapper(string connectString)
      {
        _sqlConn = new SqlConnection(connectString);
      }
    
      public override void Open()
      {
        _sqlConn.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;
      //...etc...
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering is there any way to create a temporary sandbox environment for running
I was wondering is there any way to get the last item in a
I'm wondering is there any way i could get UserName or UserId for current
I was wondering if there is any way to access variables trapped by closure
I'm wondering is there any way to automate testing of Flex applications using HP
I'm pretty sure I know the answer but I'm wondering if there's a way
Please consider the following scenario, There are two web applications App1 & App2. A
Okay, so I've got some C code to perform a mathematical operation which could,
I don't like Microsoft's font rendering. I've created a site for a client and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.