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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:05:20+00:00 2026-05-11T09:05:20+00:00

I’m having trouble getting LINQ to translate something into the query I need. In

  • 0

I’m having trouble getting LINQ to translate something into the query I need. In T-SQL, we do a <= and >= comparison on three columns that are CHAR(6) columns. LINQ will not allow me to do this since

Operator ‘<=’ cannot be applied to operands of type ‘string’ to ‘string’.

I have the following T-SQL query..

SELECT *  FROM [ZIPMASTER] zm WHERE zm.CORP = 12  AND '85546 ' BETWEEN zm.ZIPBEG AND zm.ZIPEND 

The above is not very LINQ freindly, since there is no support for BETWEEN. Thus, I have simplified to the following:

SELECT * FROM [ZIPMASTER] zm WHERE zm.CORP = 12 AND zm.ZIPBEG <= '85546 ' AND zm.ZIPEND >= '85546 ' 

Which I have used to create the following LINQ query:

var zipLinqQuery =     from z in db.ZIPMASTERs     where z.CORP == 12     && z.ZIPBEG <= '85546 '     && z.ZIPEND >= '85546 '     select z; List<ZIPMASTER> zips = zipLinqQuery.ToList<ZIPMASTER>(); 

C# – LINQ is not liking this query too much. I tried converting to ints and then comparing, however, in some cases the zip code might contain a letter. For example, the following expression would evaluate to true in T-SQL:

WHERE '85546B' BETWEEN '85546A' AND '85546D' 

I don’t know exactly why it works in T-SQL, but my guess is that it compares each character in the array individually by converting it to a numerical ASCII value.

Anyway, any help you all can provide is greatly appreciated. Thanks in advance.

CJAM

Solution (posted by Jon Skeet):

It appears that string.CompareTo() does in fact generate the needed T-SQL. Examples below:

var zipLinqQuery =     from z in db.ZIPMASTERs     where z.CORP == listItem.CORP     && z.ZIPBEG.CompareTo(listItem.ZIPCODE) <= 0     && z.ZIPEND.CompareTo(listItem.ZIPCODE) >= 0     select z; 

Generates the following T-SQL:

DECLARE @p0 INT, @p1 CHAR(6), @p2 CHAR(6) SET @p0 = 12 SET @p1 = '85546 ' SET @p2 = '85546 '  SELECT [t0].[CORP], [t0].[ZIPEND], [t0].[ZIPBEG], [t0].[CITY], [t0].[STATE], [t0].[CYCLE] FROM [dbo].[ZIPMASTER] AS [t0] WHERE ([t0].[CORP] = @p0) AND ([t0].[ZIPBEG] <= @p1) AND ([t0].[ZIPEND] >= @p2) 
  • 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-11T09:05:20+00:00Added an answer on May 11, 2026 at 9:05 am

    Try:

    var zipLinqQuery =     from z in db.ZIPMASTERs     where z.CORP == 12     && z.ZIPBEG.CompareTo('85546 ') <= 0     && z.ZIPEND.CompareTo('85546 ') >= 0     select z; 

    I don’t know that String.CompareTo works in LINQ to SQL, but it’s the first thing to try.

    (Normally you should use a StringComparer to specify the right type of comparison, but I suspect in this case CompareTo is the better option.)

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

Sidebar

Ask A Question

Stats

  • Questions 237k
  • Answers 237k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Did you try putting all your jars directly in the… May 13, 2026 at 6:30 am
  • Editorial Team
    Editorial Team added an answer you could try the following: $client = new SoapClient("http://host/services/some.wsdl", array('feature'… May 13, 2026 at 6:30 am
  • Editorial Team
    Editorial Team added an answer This is how I would do it (demo here)... The… May 13, 2026 at 6:30 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.