0

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.

3
  • 3
    Can we see your linq statement? Commented May 26, 2010 at 15:15
  • 1
    I hope that is not the actual LINQ code... Commented May 26, 2010 at 15:23
  • Outside of what I already explained, or otherwise? Commented May 26, 2010 at 15:26

3 Answers 3

3

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?

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

3 Comments

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: That is a nasty bug which has bitten me several times. If this has affected you too, please visit the bug report on Microsoft Connect and let Microsoft know that this bug has affected you as well.
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.
2

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

1 Comment

I thought the same thing. I checked that first and it is correct.
2

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?

1 Comment

Just tried to, but need to get permissions on this account. Will attempt to do that.

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.