Using AsEnumerable Hint

Recently I had a huge query that I wanted to execute on SQL server. However certain portion of query had to be executed in C# as linq to objects because SQL server cannot comprehend regular expression. In trying to figure how I can tell linq to SQL to not execute this portion of the query in SQL server, I discovered AsEnumerable operator. AsEnumerable operator is a hint that tells linq to SQL to execute the portion following it in memory. Here is an example of a query that gets the data for customers from SQL server but applied a regular expression filter in memory. It is not the best solution but illustrates how you can use these hints to drive what gets executed on SQL server.

image

image

  Looking at the output you will notice that our filter was not sent to SQL server because of using AsEnumerable hint on the IQueryable<Customer>.

1 Comment

  • Hi Zeeshan Hirani,

    Good take on this issue! If you think this is not the best way to do this then post your code on www.refactorcode.com and someone will find a better way!

    Thanks,
    Azam

Comments have been disabled for this content.