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 444979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:19:29+00:00 2026-05-12T21:19:29+00:00

I have a situation where a certain linq query that runs against a multi-million

  • 0

I have a situation where a certain linq query that runs against a multi-million row table is taking far to long to run. I dissected the linq query output and found that it was creating the parameters for the where clause using the wrong data type. For instance, one field was defined as a Char(12) in the database, but the parameter it was comparing against was declared as NVarChar(12). Once I changed the query to use Char instead of NVarChar, it ran sub-second as it should. Is there a way to get linq to sql to use the correct data type as defined in the .dbml file for that column? I double checked and its defined as DbType=”Char(12)” in the data context .dbml file.

  • 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-12T21:19:29+00:00Added an answer on May 12, 2026 at 9:19 pm

    You can get the command and reset the types of the parameters on it directly (to ansi-string in your case).

    http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.getcommand.aspx
    http://msdn.microsoft.com/en-us/library/system.data.dbtype.aspx

    Then you might call ExecuteReader on that command, yielding a DbDataReader. You can hand this DbDataReader to the Translate method of your datacontext, and it will give you the IEnumerable<T> that you’d expect from linq.

    http://msdn.microsoft.com/en-us/library/bb534213.aspx


    The performance problem is caused by the query parameter having a different type than the index selected by the query optimizer. What happens next is that the entire index is converted into the type of the parameter. This is done each time the query is issued – the conversion doesn’t hang around for later querying.

    I usually see this behavior when sending a collection of strings into the database:

      //this query will get correct parameter type
    db.Customers.Where(c => c.Name == "Bob")
      //this query can get incorrect parameter type
    List<string> names = new List<string>(){"Amy", "Bob"};
    db.Customers.Where(c => names.Contains(c.Name));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation: I have a certain function that runs a loop
I have situation, where running a query that filters by an indexed column in
I have situation where I need to make sure that user has completed certain
I have a situation where I have an interface that defines how a certain
I have this situation in a certain table: id | name 1 'Test' 2
I have a situation that requires me to call a certain controller when a
I have a situation where I want certain code to be executed no matter
using jython I have a situation where emails come in with different attachments. Certain
Situation: I have some persons with certain skills and they can/might belong to more
I have the following situation: A user will define a certain filter on a

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.