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

  • SEARCH
  • Home
  • 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 133303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:24:29+00:00 2026-05-11T06:24:29+00:00

Given this linq query against an EF data context: var customers = data.Customers.Where(c =>

  • 0

Given this linq query against an EF data context:

var customers = data.Customers.Where(c => c.EmailDomain.StartsWith(term)) 

You’d expect it to produce SQL like this, right?

SELECT {cols} FROM Customers WHERE EmailDomain LIKE @term+’%’ 

Well, actually, it does something like this:

SELECT {cols} FROM Customer WHERE ((CAST(CHARINDEX(@term, EmailDomain) AS int)) = 1) 

Do you know why?

Also, replacing the Where selector to:

c => c.EmailDomain.Substring(0, term.Length) == term 

it runs 10 times faster but still produces some pretty yucky SQL.

NOTE: Linq to SQL correctly translates StartsWith into Like {term}%, and nHibernate has a dedicated LikeExpression.

  • 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. 2026-05-11T06:24:30+00:00Added an answer on May 11, 2026 at 6:24 am

    The reason is that CharIndex is a lot faster and cleaner for SQL to perform than LIKE. The reason is, that you can have some crazy ‘LIKE’ clauses. Example:

    SELECT * FROM Customer WHERE EmailDomain LIKE 'abc%de%sss%' 

    But, the ‘CHARINDEX’ function (which is basically ‘IndexOf’) ONLY handles finding the first instance of a set of characters… no wildcards are allowed.

    So, there’s your answer 🙂

    EDIT: I just wanted to add that I encourage people to use CHARINDEX in their SQL queries for things that they didn’t need ‘LIKE’ for. It is important to note though that in SQL Server 2000… a ‘Text’ field can use the LIKE method, but not CHARINDEX.

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

Sidebar

Related Questions

Given this linq query from c in context.Customers from o in c.Orders where c.City
Given the following LINQ to SQL query: var test = from i in Imports
I'm binding the results of a linq-to-sql query to a datagridview. This works fine
Given this data set: ID Name City Birthyear 1 Egon Spengler New York 1957
I have create a LINQ-to-SQL project in Visual Studio 2010 using Dynamic Data. In
I would like to query data given an array to filter by via WCF
I'm having trouble rewriteing a complex sql query as linq. -- gets the SpecificationAttributeOptionIDs
I'm new to LINQ , and I'm having trouble organizing this query to return
I need equivalent LINQ to SQL query for the query written in access database.
Say I have the following query OData Linq Query (run against http://odata.netflix.com/v2/Catalog ): Genres.Where(x=>x.Name==Adventures)

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.