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

The Archive Base Latest Questions

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

I’m trying to write Linq-To-SQL queries in LinqPad to help migrate users from old

  • 0

I’m trying to write Linq-To-SQL queries in LinqPad to help migrate users from old tables to new tables. Part of this migration is storing all addresses in a separate table. I’m using the following query to determine if a user’s address exists in the new table (so I don’t have duplicate entries):

var addresses = from a in Addresses where ((u.Street_address == null && a.Street1 == null) || (u.Street_address != null && a.Street1 != null && a.Street1.ToLower() == u.Street_address.ToLower())) 
                                && ((a.City == null && u.City == null) || (a.City != null && u.City != null && a.City.ToLower() == u.City.ToLower())) 
                                && ((a.State == null && u.State == null) || (a.State != null && u.State != null && a.State.ToLower() == u.State.ToLower())) 
                                && ((a.Zip == null && u.Zipcode == null) || (a.Zip != null && u.Zipcode != null && a.Zip.ToLower() == u.Zipcode.ToLower()))
                                select a;

Here, ‘u’ represents an old user. Some addresses in the old table contain null entries for the Street_address, City, State, and/or Zipcode. Also, some addresses are duplicates, except for casing (hence the ToLower()).

Despite checking for null parameters in the query, I still get a NullReferenceException if any of the user’s address parameters are null.

Am I doing something wrong? Or is there a better way to accomplish what I need?

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

    UPDATE:

    Hmmm, looks like this is more complex than I originally thought. Turns out String.Equals with a StringComparison overload is not supported by Linq-to-SQL. But, the fact that you get the error means that Linq-to-SQL is trying to take the entire expression and turn it into SQL. Which in turn means that all comparisons will happen according to the native collation of the database– which by default is case-insensitive. So even though Linq-to-SQL won’t support case-insensitive comparisons, you probably don’t need case-insensitive comparisons since you can rely on SQL Server doing them by default.

    So, provided you haven’t changed the collation of your string columns in your table from case-insensitive (the default) to case-sensitive, the following code should work:

    var addresses = from a in Addresses 
                    where String.Equals (u.Street_address, a.Street1)
                          && String.Equals (u.City, a.City)
                          && String.Equals (u.State, a.State)
                          && String.Equals (u.ZipCode, a.Zip)
                    select a;
    

    It’s possible this may work too:

    var addresses = from a in Addresses 
                    where u.Street_address == a.Street1
                          && u.City == a.City
                          && u.State == a.State
                          && u.ZipCode == a.Zip
                    select a;
    

    But, based on my reading of this MSDN article (excerpted below), I suspect that only using == (instead of String.Equals() may not work) :

    Null semantics

    LINQ to SQL does not
    impose null comparison semantics on
    SQL. Comparison operators are
    syntactically translated to their SQL
    equivalents. For this reason, the
    semantics reflect SQL semantics that
    are defined by server or connection
    settings. For example, two null values
    are considered unequal under default
    SQL Server settings, but you can
    change the settings to change the
    semantics. LINQ to SQL does not
    consider server settings when it
    translates queries.

    A comparison with the literal null is
    translated to the appropriate SQL
    version (is null or is not null).

    In other words, if I’m reading this MSDN text correctly, it sounds like Linq-to-SQL translates == into = in T-SQL, while (as your experiment showed) String.Equals is translated correctly as a check for IS NULL followed by a check using =. If you have a chance to test just ==, I’d be interested to see whether Linq-to-SQL emits the IS NULL checks or not.

    Given the complexity here (Linq-to-SQL translating C# into SQL, and results back to C#) your best bet in cases like this is to try multiple variations (e.g. == vs. Equals()) and pick the one that works, since there are enough moving parts that it’s hard to anticipate ahead of time which variation will work best.

    OLD ANSWER (ignore this):

    Consider using the static String.Equals method instead of == and ToLower(). You’ll avoid the null-reference problems (and simplify your code) because nulls are OK to pass into that method and it supports a case-insensitive check.

    var addresses = from a in Addresses 
                    where String.Equals (u.Street_address, a.Street1, StringComparison.OrdinalIgnoreCase)
                          && String.Equals (u.City, a.City, StringComparison.OrdinalIgnoreCase)
                          && String.Equals (u.State, a.State, StringComparison.OrdinalIgnoreCase)
                          && String.Equals (u.ZipCode, a.Zip, StringComparison.OrdinalIgnoreCase)
                    select a;
    

    Although if your database is already case-insensitive, depending on how Linq-to-SQL splits the work between SQL and C# code, you may not need the insensitivity check at all– although if it were me, I’d rather be safe and enure you always enforce the case check.

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

Sidebar

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.