views:

73

answers:

3

I have a LINQ to SQL query that when executed does not return any data. However, when I debug, I can take the generated SQL query, plug in the values for the variables, and run it in SQL Management Studio to get the record I'm expecting. I'm taking the values for the variables while in debug mode as well.

Has anyone experienced something like this before?

As requested, LINQ statement (edited table and column names):

var q1 = from rr in db.ABC
         from rd in db.DEF
         where rr.a == rd.b
         where rr.c == rd.c
         where rr.d.Equals(id)
         where rr.c.Equals(anotherId)
         select new
         {
             rr.d, rr.x, rr.a, 
             rr.y, rr.z, rr.v, 
             rr.e, rd.r
         };

var r1 = q1.Single();

I'm using very similar queries in other places with success.

+2  A: 

I would double check the connection string that LINQ to SQL is using. It might just be hitting the wrong database.

smaclell
I thought the same thing. I checked that first and it is correct.
Chris Stewart
+2  A: 

Have you run Profiler to make sure the values you think the stament is going to have are actually inthe statment that was sent to the server?

HLGEM
Just tried to, but need to get permissions on this account. Will attempt to do that.
Chris Stewart
+3  A: 

Run the SQL Server profile, take the EXACT query it's running (you shouldn't have to "plug in" any values), and run that.

Are you still obtaining results?
Is it plugging in the correct values?
Are you querying a nullable value?
Could you show us the code and generated query?

BlueRaja - Danny Pflughoeft
It ended up being an issue with a nullable value. If you have suggestions on how I may improve my query, structurally, please do clue me in.
Chris Stewart
@Chris: That is a nasty bug which has bitten me several times. If this has affected you too, please visit [the bug report](https://connect.microsoft.com/data/feedback/details/545491/linq-to-entities-incorrect-handling-of-null-variables-in-where-clause) on Microsoft Connect and let Microsoft know that this bug has affected you as well.
BlueRaja - Danny Pflughoeft
If you wanted to tweak the query to clean it up consider using a join instead of the first two where clauses and combining your remaining where clauses into a single statement.
smaclell