1

Can anyone explain to me why these two are different functionally?

The first returns null on my sample data, and the second returns the expected result. I put the + @"" part in to avoid calling replace on a null string.

Code always returns null on my sample data:

to_follows_contact = db.CONTACTs.Where(n =>
    n.ACCOUNTID == account.ACCOUNTID
    && (
        (n.FIRSTNAME + n.MIDDLENAME + n.LASTNAME + @"").Replace(@" ", @"") 
                                                            == @"TOFOLLOW"
        ||
        (n.FIRSTNAME + n.MIDDLENAME + n.LASTNAME + @"").Replace(@" ", @"") 
                                                            == @"FOLLOWTO"
    )
).FirstOrDefault();

Code returns correct contact on my sample data:

foreach (CONTACT n in db.CONTACTs.Where(n => n.ACCOUNTID == account.ACCOUNTID))
{
    string ContactName = (n.FIRSTNAME + n.MIDDLENAME + n.LASTNAME + @"")
                         .Replace(@" ", @"");
    if (ContactName == @"TOFOLLOW" || ContactName == @"FOLLOWTO")
        to_follows_contact = n;
}
3
  • So far, I'm as puzzled with your problem as you are... I just wanted to point out that the @ signs for C# verbatim strings are not necessary in this case. Commented Jan 19, 2011 at 8:47
  • 2
    Try taking a look at the generated SQL, might be an issue there. Commented Jan 19, 2011 at 9:00
  • @Thorarin: I just got into the habit of always using @ in front of strings unless I explicitly wanted escape characters. That way, I don't have to worry about windows paths stored as strings (which I do a lot). Commented Jan 19, 2011 at 10:50

2 Answers 2

5

What's happening, is that the first code sample actually solves the problem entirely in T-SQL. Unfortunately, the translation from C# to SQL isn't entirely transparent.

SQL handles null values differently* from .NET. SELECT 'Foo' + NULL + 'Bar' will return NULL. You will need to use COALESCE or something like it. In LINQ to SQL, that translates to:

(n.FIRSTNAME ?? "" + n.MIDDLENAME ?? "" + n.LASTNAME ?? "").Replace(" ", "")

If you use that, it should work fine.

If you're interested, you can use db.GetCommand(IQueryable<T>) to get the generated SQL statement. It's also available when you hover over the IQueryable variable in a piece of source code while debugging.

*: this acually depends on some database options that you can set, but this is the default behavior.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice. I haven't come across the ?? operator in C# before.
@Jonathan: it's aptly named the null-coalescing operator: msdn.microsoft.com/en-us/library/ms173224.aspx
1

At a guess, the generated SQL will concatenate the various strings, however, if one of them is null, then you'll always get NULL returned from the concatenation, which will fail to pass your equality test.

In the code, the strings will always be empty, which will then work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.